簡體   English   中英

SQL查詢:如何將計數函數結果組合到選擇查詢中?

[英]SQL Query: How do you combine count function result into a select query?

select distinct Franchise.FranchiseName, Franchise.Initials, Franchise.StoreNo, AccountCancellation_Process.Store_Num 
FROM FranchiseData
INNER JOIN AccountCancellation_Process
on FranchiseData.StoreNo = AccountCancellation_Process.Store_Num

select count(*) from AccountCancellation_Process where Store_Num = '1234' 
select count(*) from AccountCancellation_Process where Store_Num = '1234' and Progress is not null

我想將AccountCancellation_Process的count(*)合並到上面的內部連接語句中,以便查詢將給我結果,FranchiseName,Initials,Franchise表中的StoreNo和AccountCancellation_Process中的Store_Num的結果與總記錄和不帶Progress列的總記錄一起空值。

如何將查詢結果與計數功能結果結合在一起?

謝謝。

這樣,我想就是您想要的。 我創建了兩個與表值相關的子查詢,以基於內部聯接表中存儲的數字獲取數據。 然后我根據商店編號加入他們。 這樣,獨特的將工作。 但是,您也可以只使用相關的子查詢就可以對選擇部分進行計數。 我擔心區別可能不會起作用。

SELECT DISTINCT Franchise.FranchiseName, Franchise.Initials, Franchise.StoreNo, acp.Store_Num, total_count.Total_Count, progress_count.Progress_Count
FROM FranchiseData
     INNER JOIN AccountCancellation_Process AS acp ON (FranchiseData.StoreNo = acp.Store_Num)
     INNER JOIN (SELECT Store_Num, COUNT(*) AS Total_Count FROM AccountCancellation_Process WHERE Store_Num = acp.Store_Num) AS total_count ON (acp.Store_Num = total_count.Store_Num)
     INNER JOIN (SELECT Store_Num, COUNT(*) AS Progress_Count FROM AccountCancellation_Process WHERE Store_Num = acp.Store_Num AND Progress IS NOT NULL) AS progress_count ON (acp.Store_Num = progress_count.Store_Num)

別名count(*)然后使用Sum(alias)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM