34,590
社区成员
发帖
与我相关
我的任务
分享
ORDER BY CASE WHEN customer ='其它' THEN 1 ELSE 0 END,qty DESC
with list as(
select customer='a',qty=100 union all
select customer='a',qty=88 union all
select customer='b',qty=88 union all
select customer='b',qty=99 union all
select customer='c',qty=309 union all
select customer='c',qty=58 union all
select customer='d',qty=28 union all
select customer='e',qty=88 union all
select customer='f',qty=88
),datalist as(
select customer,qty=sum(qty)
,rid=ROW_NUMBER()over (order by sum(qty) desc)
from list
group by customer
)
SELECT * FROM (
select customer=case when rid between 1 and 3 then customer else '其它' end
,qty=sum(qty)
from datalist
group by case when rid between 1 and 3 then customer else '其它' END
) AS t
ORDER BY CASE WHEN customer = '其它' THEN 1 ELSE 0 END,qty DESC
with list as(
select customer='a',qty=100 union all
select customer='a',qty=88 union all
select customer='b',qty=88 union all
select customer='b',qty=99 union all
select customer='c',qty=309 union all
select customer='c',qty=58 union all
select customer='d',qty=28 union all
select customer='e',qty=88 union all
select customer='f',qty=88
),datalist as(
select customer,qty=sum(qty)
,rid=ROW_NUMBER()over (order by sum(qty) desc)
from list
group by customer
)
select customer=case when rid between 1 and 3 then customer else '其它' end
,qty=sum(qty)
from datalist
group by case when rid between 1 and 3 then customer else '其它' end
ORDER BY CASE WHEN customer = '其它' THEN 1 ELSE 0 END,qty DESC