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;
  • 相关阅读:
    unittest生成html测试报告
    excel类封装
    023-linux(2)
    016-WebDriver API(2)
    015-WebDriver API
    014-unittest扩展
    013- unittest单元测试框架
    011-python列表,元组,字典的用法
    010-利用Selenium+python自动输入博客账号密码登录
    009-python一些问题整理
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13624577.html
Copyright © 2011-2022 走看看