我试图创建一个SQL查询以计算前6个选择中[Over] ='Y'的次数,并且合并查询中出现错误。
查询以获得前6名:
SELECT TOP 6 [Number]
,[Over]
,[Goal]
FROM [Test_Hermes].[dbo].[Test]
ORDER BY Number desc
查询计数:
SELECT COUNT([Over])
FROM [dbo].[Test]
WHERE [OVER] = 'Y';
合并两个查询:
SELECT COUNT([Over])
FROM (SELECT TOP 6 [Number]
,[Over]
,[Goal]
FROM [Test_Hermes].[dbo].[Test]
ORDER BY Number desc)
WHERE [OVER] = 'Y';
任何帮助将不胜感激! 谢谢
答案 0 :(得分:1)
请显示此示例。
SELECT
[Number]
,[Over]
,[Goal]
, SUM( If(OVER= 'Y',1,0)) AS CNT
FROM [Test_Hermes].[dbo].[Test]
ORDER BY Number desc
LIMIT 1,6;
答案 1 :(得分:0)
您不能在子查询中使用订单。
SELECT COUNT([t1].[Over])
FROM (SELECT TOP 6 [Number]
,[Over]
,[Goal]
FROM [Test_Hermes].[dbo].[Test]) as t1
WHERE [OVER] = 'Y' ;
如果您需要确定前6名的订单,请使用ROW_NUMBER()
SELECT COUNT([t1].[Over])
FROM (SELECT ROW_NUMBER() OVER(PARTITION BY [Number] ORDER BY [Number] DESC) as 'Rank'
,[Number]
,[Over]
,[Goal]
FROM [Test_Hermes].[dbo].[Test]) as t1
WHERE [t1].[OVER] = 'Y' AND [t1].[Rank] <= 6;