SQLserver中取众位数的写法

时间:2022-03-19 12:10:26

取众位数:先按个数倒排序,再取第一条
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

SQLserver中取众位数的写法

按个数排序:
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

SQLserver中取众位数的写法