简体   繁体   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

INPUT DATA:输入数据:

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 Data: Output 资料:

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

You can use CASE expression if your DBMS has support for it.如果您的 DBMS 支持,您可以使用CASE表达式。

This statement works fine at least in PostgreSQL:此语句至少在 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