我有一个下面的数据集,我正在尝试每个CID
的最大出现次数 OID
。
IF OBJECT_ID('TEMPDB..#SS',N'U') IS NOT NULL
DROP TABLE #SS
GO
SELECT * INTO #SS FROM (
SELECT 1 AS OID,501 AS CID
UNION ALL
SELECT 1 AS OID,503 AS CID
UNION ALL
SELECT 1 AS OID,502 AS CID
UNION ALL
SELECT 1 AS OID,501 AS CID
UNION ALL
SELECT 1 AS OID,501 AS CID
UNION ALL
SELECT 2 AS OID,502 AS CID
UNION ALL
SELECT 2 AS OID,502 AS CID
UNION ALL
SELECT 2 AS OID,502 AS CID
UNION ALL
SELECT 2 AS OID,501 AS CID
UNION ALL
SELECT 1 AS OID,503 AS CID
)A
GO
在上面的示例数据集中,每个出现时间最多的CID
需要获得2 OID
。预期结果可能是:
OID CID
1 501
1 503
2 501
2 502
这不能与SQL Select top frequent records重复,因为我需要此子查询,并且SQL Server无法在子查询中接受ORDER BY
,最终我需要一种排名函数来解决我的问题问题。所提供的链接中未使用排名功能。
答案 0 :(得分:3)
您可以利用ROW_NUMBER轻松完成此操作。感谢Tab Alleman对您的要求的澄清。
create-react-app