zoukankan      html  css  js  c++  java
  • [Postgres] Subquery Dynamic Datasets in SQL

    SQL is dynamic enough to handle queries within queries. These inner queries are called subqueries and they can be used in many different sections of another query. In this video we will use subqueries within a where clause, a join statement, and as a column value.

    $ postgres=# select create_date, first_name from Users where create_date = (select min(create_date) from Users);
    
         min     | first_name
     ------------+------------
      2018-06-06 | tyler 
    (1 row)
    $ postgres=# select total, first_name from Users us inner join (select count(user_handle) as total, user_handle from Purchases group by user_handle) p on p.user_handle = us.user_handle;
       total     | first_name
     ------------+------------
             1   | danny 
             2   | mary 
    (2 rows)
    $ postgres=# select user_handle, sku, (select avg(quantity) from Purchases) from Purchases;
                  user_handle             |                sku                    |       avg
    --------------------------------------+---------------------------------------+--------------- 
     6ab3b2d2-8e02-890c-bb6d-61a67cd43f31 | 2839f831-f82c-faj3-aof3-fj28ddks39ek  | 1.50000000000
     a0eebc99-9c0b-42f8-g3eh-6bb9bd380a11 | a0eebc99-9c0b-42f8-bb6d-6bb9bd380a11  | 1.50000000000
     2839f831-f82c-faj3-aof3-fj28ddks39ek | a0eebc99-9c0b-42f8-bb6d-6bb9bd380a11  | 1.50000000000
     a0eebc99-9c0b-42f8-g3eh-6bb9bd380a11 | 2839f831-f82c-faj3-aof3-fj28ddks39ek  | 1.50000000000
    (4 rows)
    select user_handle, sku, (select avg(quantity) from Purchases where user_handle = p.user_handle and sku = p.sku) from Purchases p group by user_handle, sku;
  • 相关阅读:
    ubuntu如何以删除文件夹?
    Ubuntu下安装lrzsz
    SSH服务器拒绝密码检测
    ubuntu下安装、启动和卸载SSH
    VirtualBox下Ubuntu利用桥接方式上网
    Python网络编程笔记二
    Python网络编程笔记一
    Python反射笔记
    Python之time模块和datatime模块
    Python正则表达式之findall疑点
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13624577.html
Copyright © 2011-2022 走看看