分組資料

肥梁發表於2024-05-29

指定分組後,對每個分組裡的資料進行聚集

  1. select vend_id, count(*) as num_prods from products group by vend_id;

  2. 過濾分組
    select cust_id, count(*) from orders group by cust_id having count(*) > =2;

注意: where是在分組前進行過濾,having是在分組後進行過濾

  1. 價格>=10, 具有2個產品及其以上的供應商。
    select vend_id, count(*) as num_prods from products where prod_price >=10 group by vend_id having count(*) >=2;

  2. 總計訂單價格大於等於50的訂單號和總計價格
    select order_num, sum(quantity * item_price) as ordertotal from orderitems group by order_num having sum(quantity * item_price) >=50;

  3. select order_num, sum(quantity * item_price) as ordertotal from orderitems group by order_num having sum(quantity * item_price) >=50 order by ordertatal

相關文章