样本表:
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
答案 0 :(得分:2)
Select * from
(Select CusID,row_number() over (order by CusID) r,
count(case Status when 'Paid' then 1 else null end)
AS NumOfOrdersPaid from Table1 Group By CusID) X
where x.r between 1 and 2;
答案 1 :(得分:0)
SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY CusID ASC) AS Row, CusID, NumOfOrdersPaid FROM MyTable) As List