2016-12-03 6 views
2

ich die folgende Tabelle haben:zusammenfassend eine Tabelle in SQL

MedicineID Type PlantId category 
1223456  ABC  P1   A 
7821632  DEF  P2   B 
5436710  GHI  P1   D 
ABC  P3   A 
4321793  THE  P4   D 
7285743  ABC  P1   B 
4143521  DEF  P3   A 
5234345  GHI  P2   D 
5132451  FGE  P1   B 
1235432  REF  P4   A 
5652315  GHI  P3   D 
6733115  ABC  P2   B 
7752242  DEF  P3   A 
2652225  GHI  P2   D 
6242352  ABC  P1   B 

Ich habe 7 verschiedene Arten von Medizin = [ABC DEF GHI DIE FGE REF XYZ ] Und jeder kann sein in 4 verschiedenen Kategorien. Die Medikamente werden in 4 verschiedenen Pflanzen hergestellt. Alle Pflanzen haben die Fähigkeit, alle Medikamente herzustellen. Ich möchte wissen, wie viele verschiedene Medikamente in jeder Pflanze für jeden Typ und jede Kategorie hergestellt werden. Meine resultierende Tabelle soll so aussehen.

Plant Type  A  B  D 
P1  ABC  1  2  0 
P1  DEF  0  0  0 
P1  GHI  0  0  1 
P1  THE  0  0  0 
P1  FGE  0  1  0 
P1  REF  0  0  0 
P1  XYZ  0  0  0 
P2  ABC  0  1  0 
P2  DEF  0  1  0 
P2  GHI  0  0  2 
P2  THE  0  0  0 
P2  FGE  0  0  0 
P2  REF  0  0  0 
P2  XYZ  0  0  0 
P3  ABC  1  0  0 
P3  DEF  2  0  0 
P3  GHI  0  0  1 
P3  THE  0  0  0 
P3  FGE  0  0  0 
P3  REF  0  0  0 
P3  XYZ  0  0  0 
P4  ABC  0  0  0 
P4  DEF  0  0  0 
P4  GHI  0  0  0 
P4  THE  0  0  1 
P4  FGE  0  0  0 
P4  REF  1  0  0 
P4  XYZ  0  0  0 

Ich bin unsicher, welche Befehle sogar verwendet werden, um zu beginnen. Jede Hilfe in die richtige Richtung wird geschätzt.

Antwort

5

Wenn die Anzahl der Kategorien festgelegt ist, können Sie einfach bedingte Aggregation verwenden.

select tplant.plantid,ttype.type, 
count(case when t.category='A' then t.plant end) A, 
count(case when t.category='B' then t.plant end) B, 
count(case when t.category='D' then t.plant end) D 
from (select distinct type from typetable) ttype --replace typetable with the table that has all the types 
cross join (select distinct plantid from tablename) tplant 
left join tablename t on t.plantid=tplant.plantid and t.type=ttype.type 
group by tplant.plantid,ttype.type 
+0

Aber XYZ erscheint nicht in der ursprünglichen Tabelle ich es in einer separaten Liste haben aber die Anzahl der Kategorien festgelegt sind – Morpheus

+0

@Morpheus... .. siehe edit .. –

+0

Vielen Dank !. Wenn es nicht zu viel Ärger ist, was ist, wenn ich die Pflanzen auch in einer anderen Liste habe.Ich meine, wenn diese bestimmte Pflanze nicht in der Pflanze ist, alle Einträge wird 0 sein – Morpheus

2

@ vkps Antwort ist korrekt. Im Folgenden haben könnte etwas bessere Leistung (einen Index für tplant(type, plantid, category) vorausgesetzt:

select p.plantid, t.type, tp.A, tp.B, tp.D 
from (select distinct type from typetable) t cross join 
    (select distinct plantid from tablename) p outer apply 
    (select sum(case when t.category = 'A' then 1 else 0 end) as A, 
      sum(case when t.category = 'B' then 1 else 0 end) as B, 
      sum(case when t.category = 'D' then 1 else 0 end) as D 
     from tplant tp 
     where tp.type = t.type and tp.plantid = p.plantid 
    ) tp; 
Verwandte Themen