2017-07-27 1 views
2

Ich schaute online und konnte keine Antwort auf das folgende Problem finden.Einzelne Gruppenfunktion - Produktanzahl nach Kategorie und Region

Ich möchte die folgende Tabelle erstellen. Ich könnte alle Spalten außer Count_product_A und count_product_B erstellen.

Link zu Tabelle:

Link to table

Link zu Tabelle 1:

Linke to table 1

Select 
    Region_flag, type, count(total product) 
    ,sum(Case when product in ('A') then count(total product) END) as count_product_A 
    ,sum(Case when product in ('B') then count(total product) END) as count_product_B 
FROM Table1 
Group by 
    Region_flag, type 
+0

was Willst du count_product_A und _B sein? Die Summe eines COUNT? Oder nur die Anzahl der Produkte? – RealCheeseLord

+0

Ja die Summe einer Zählung – Soph

+0

können Sie einen Teil Ihrer Tabelle hinzufügen? – RealCheeseLord

Antwort

2

Diese Abfrage:

SELECT 
    region_flag, type, 
    count(1) AS count_total, 
    sum(CASE WHEN product IN ('A') THEN 1 ELSE 0 END) AS count_product_A, 
    sum(CASE WHEN product IN ('B') THEN 1 ELSE 0 END) AS count_product_B 
FROM 
    table1 
GROUP BY 
    region_flag, type 
+0

Es funktioniert! Ich danke dir sehr! – Soph

+0

bitte als Antwort markieren – RealCheeseLord