我如何看到表中最高值的计数结果,而不是SQL中的整个表
我正在为SSMS18中的公用事业账单创建数据库,并且我正在尝试检索最高账单前10个月中特定月份的计数。如何将计数功能限制为仅计数排的最高数?
表设计
CREATE TABLE Electric (
[date] date NOT NULL,
electric_bill_amount smallmoney);
我可以通过添加一个语句来限制结果
SELECT DISTINCT DATENAME(MONTH, [date]) AS MONTH,
COUNT(DATENAME(MONTH, [date])) AS [Frequency]
FROM Electric
WHERE electric_bill_amount > 104
GROUP BY DATENAME(MONTH, [date])
ORDER BY [Frequency] DESC
10个最高值。
结果应该看起来像这样
August 3
September 3
July 2
January 1
December 1
I am creating a Database for my utility bills in SSMS18, and I am trying to retrieve the count for the occurrence of a specific month in the top 10 months with the highest bills. How do I restrict the COUNT feature to only count the top number of rows?
Table design
CREATE TABLE Electric (
[date] date NOT NULL,
electric_bill_amount smallmoney);
I can limit the results by adding a WHERE statement
SELECT DISTINCT DATENAME(MONTH, [date]) AS MONTH,
COUNT(DATENAME(MONTH, [date])) AS [Frequency]
FROM Electric
WHERE electric_bill_amount > 104
GROUP BY DATENAME(MONTH, [date])
ORDER BY [Frequency] DESC
but I would like to my query to be more dynamic and therefore only have the count statement use the months with the top 10 highest values.
Results should look something like this
August 3
September 3
July 2
January 1
December 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)