SQLserver中取众位数的写法

取众位数:先按个数倒排序,再取第一条
SELECT * FROM
(
select a.billid,a.DemandVoltage,count(1) as RecordCount,
Row_NUMBER() OVER(PARTITION BY billid ORDER BY count(1) DESC) AS SN
from ETL_SingleCharging201805 a with(nolock)
WHERE RowKey LIKE '00000058-bca0-4559-8f8f-2fc3b61eb01c%'
OR Rowkey LIKE '000001b7-3e26-47fd-9a67-b177750d5147%'
group by a.billid,a.DemandVoltage
) b
WHERE b.billid IN ('00000058-bca0-4559-8f8f-2fc3b61eb01c',
'000001b7-3e26-47fd-9a67-b177750d5147') AND b.SN=1

按个数排序:
select a.billid,a.DemandVoltage,count(1) as RecordCount, Row_NUMBER() OVER(PARTITION BY billid ORDER BY count(1) DESC) AS SN
from ETL_SingleCharging201805 a with(nolock)
WHERE RowKey LIKE '00000058-bca0-4559-8f8f-2fc3b61eb01c%' OR Rowkey LIKE '000001b7-3e26-47fd-9a67-b177750d5147%'
group by a.billid,a.DemandVoltage

 

posted @ 2018-05-25 19:03  闲汉  阅读(290)  评论(0编辑  收藏  举报