简体   繁体   中英

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:

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

You can use CASE expression if your DBMS has support for it.

This statement works fine at least in 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM