样品表:
CusID Order Status
3001 Hotdog Paid
3001 Sausage Paid
3001 Burger Unpaid
3002 Sausage Paid
3003 Burger Paid
3003 Hotdog Paid
3002 Hotdog Paid
3001 Burger Paid
3002 Burger Unpaid
期望的输出
Row CusID NumOfOrdersPaid
1 3001 3
2 3002 2
我试过这个:
SELECT *
FROM (
SELECT row_number() OVER (
ORDER BY CusID
) row
,count(CASE STATUS
WHEN 'Paid'
THEN 1
ELSE NULL
END) AS NumOfOrdersPaid
FROM Orders
) X
WHERE x.r BETWEEN 1
AND 2
GROUP BY CusID