2013-06-29 12 views
10

ich wie eine Tabelle untenSQL Countif Funktion

Type of Station | Broadcast Management 
----------------+------------------------- 
Full Power  | Sinclair Broadcast Group 
Full Power  | Sinclair Broadcast Group 
LPTV cable  | Sinclair Broadcast Group 
LPTV no cable | Sinclair Broadcast Group 

Jetzt möchte ich eine Abfrage auszuführen, die Ergebnis bringen wird sieht aus wie unter

Broadcast Management  | Full Power | LPTV cable | LPTV no cable 
---------------------------+------------+------------+-------------- 
Sinclair Broadcast Group | 2   |  1  | 1 

Kann jemand bitte helfen Sie mir, wie Sie diese Abfrage schreiben

Antwort

21

Es gibt keine einzige SUMIF oder COUNTIF.

aber Sie haben SUM oder COUNT und eine IFCASE mit ...

SELECT 
    [Broadcast Management], 
    SUM(CASE WHEN [Type of Station] = 'Full Power' THEN 1 ELSE 0 END) AS [Full Power], 
    SUM(CASE WHEN [Type of Station] = 'LPTV Cable' THEN 1 ELSE 0 END) AS [LPTV Cable], 
    SUM(CASE WHEN [Type of Station] = 'LPTV No Cable' THEN 1 ELSE 0 END) AS [LPTV No Cable] 
FROM 
    yourTable 
GROUP BY 
    [Broadcast Management] 

Für zählt, können Sie die ELSE Rückkehr NULL als die Anzahl der 1, 2, 4, NULL3 ist machen.

+0

ausgezeichnet. Danke @ MatBailie –