簡體   English   中英

在單個 sql 查詢中查找銷售額大於 2000 的訂單總數和銷售總數

[英]FInd the total number of orders and total number of sales which are having sale value greater than 2000 in a single sql query

輸入數據:

Customer_ID order_number    order_value
1                  1      500
1                  2      300
1                  3      2400
1                  4      2123
2                  5      300
2                  2      2400

Output 資料:

Customer ID no. of orders   valuegt2000
1             4                   2
2             2                   1

如果您的 DBMS 支持,您可以使用CASE表達式。

此語句至少在 PostgreSQL 中運行良好:

select
  customer_id as "customer id",
  count(order_number) as "no. of orders",
  sum(
    case
      when order_value > 2000 then 1
      else 0
    end
  ) as valuegt2000
from
  my_table
group by
  customer_id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM