[英]How do you count the rows in a SQL query which already used count, group by, and having before?
例如,使用這個問題的答案:
如何選擇提交超過 10 次的所有用戶 “如何選擇提交超過 10次的所有用戶。”
select userId
from submission
group by userId
having count(submissionGuid) > 10
假設現在我想知道這條 sql 語句輸出的行數。 計算行數的解決方案的可擴展性如何?
先前發布的示例中的小錯誤,需要子查詢的表名的別名:
select count(*) from
(select userId
from submission
group by userId
having count(submissionGuid) > 10) t
我不確定可擴展性,但這是解決方案。 如果這對您來說擴展性不夠好,您需要考慮重大的設計更改,例如可能跟蹤那些在單獨的表格中提交超過 10 個提交的人,您通過填充提交的應用程序更新該表格。 或許多其他可能的解決方案。
嵌套查詢:
select count(*) from
(select userId
from submission
group by userId
having count(submissionGuid) > 10) n
編輯以合並 mbrierst 關於嵌套子查詢需要別名(末尾的“n”)的評論。 Oracle 不需要,但 SQL Server 需要。 隨意添加有關其他數據庫平台使用情況的評論。
在 SQL Server 中你可以做
select @@ROWCOUNT
緊跟在您發布的查詢之后。
你可以試試這個方法:我希望能幫助你。
select top(1) row_number() over(partition by count(userId) order by count(userId)) as RowNumber
from submission
group by userId
having count(submissionGuid) > 10
order by userId desc
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.