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;
  • 相关阅读:
    软件工程(2019)第二次作业
    软件工程(2019)第一次作业
    Java基础篇之Java特性
    软件工程(2019)结对编程第二次作业
    软件工程(2019)结对编程第一次作业
    软件工程(2019)第三次作业
    软件工程(2019)第二次作业
    软件工程(2019)第一次作业
    结对编程作业 2
    结对编程作业 1
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13624577.html
Copyright © 2011-2022 走看看