在我之后我ORDER BY cnt DESC
的结果是
fld1 cnt
A 9
E 8
D 6
C 2
B 2
F 1
我需要显示前 3 个,其余的总和为“其他”,如下所示:
fld1 cnt
A 9
E 8
D 6
other 5
编辑:
谢谢大家的意见。如果您看到实际的陈述,也许会有所帮助:
SELECT
CAST(u.FA AS VARCHAR(300)) AS FA,
COUNT(*) AS Total,
COUNT(CASE WHEN r.RT IN (1,11,12,17) THEN r.RT END) AS Jr,
COUNT(CASE WHEN r.RT IN (3,4,13) THEN r.RT END) AS Bk,
COUNT(CASE WHEN r.RT NOT IN (1,11,12,17,3,4,13) THEN r.RT END ) AS Other
FROM R r
INNER JOIN DB..RTL rt
ON r.RT = rt.RTID
INNER JOIN U u
ON r.UID = u.UID
WHERE rt.LC = 'en'
GROUP BY CAST(u.FA AS VARCHAR(300))--FA is ntext
ORDER BY Total DESC
生成的结果有 19 条记录。我需要显示前 5 名并将其余的总结为“其他 FA”。我不想用这种语句从一个选择中选择一个选择。我更多的是寻找一些 SQL 函数。也许 ROW_NUMBER 是个好主意,但我不知道在这种情况下如何应用它。