2009-04-09 10 views
1
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 

Ich mag den count (*) von AccountCancellation_Process in die obigen inneren kombiniere Join-Anweisung so die Abfrage mir gibt das Ergebnis FranchiseName, Initialen, StoreNo von Franchise-Tabelle und Store_Num vom AccountCancellation_Process mit insgesamt Aufzeichnungen und Gesamt Aufzeichnung mit Progress-Spalte nicht null.SQL-Abfrage: Wie kombinierst du Zählfunktionsergebnis in eine Auswahlabfrage?

Wie kombinieren Sie das Abfrageergebnis mit dem Zählfunktionsergebnis?

danke.

Antwort

1

Alias ​​the count (*), dann verwenden Sie eine Sum (Alias)

2

Mag ich denke, ist das, was Sie wollen. Ich erstellte zwei korrelierte Unterabfragen für Tabellenwerte, um die Daten basierend auf der gespeicherten Zahl in der inneren Join-Tabelle abzurufen. Dann schließe ich mich ihnen basierend auf der Filialnummer an. Auf diese Weise wird das Besondere funktionieren. Sie können jedoch auch die Anzahl im Auswahlbereich mit nur korrelierten Unterabfragen ermitteln. Ich war besorgt, dass das Besondere vielleicht nicht funktionieren würde.

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)