2016-10-27 1 views
3

Ich habe verstehen voll versucht, verbindet sich mit dem folgende Beispiel, das ich in SQL ServerVerständnis schließt sich mit einem Beispiel

DECLARE @tablePlace TABLE (ID INT, someplace varchar(10)) 
DECLARE @tableType TABLE (ID INT, sometype varchar(10)) 
DECLARE @tableOrders TABLE (ID INT, type int , place int) 

INSERT INTO @tablePlace (ID, someplace) values (1,'Place A') 
INSERT INTO @tablePlace (ID, someplace) values (2,'Place B') 

INSERT INTO @tableType (ID, sometype) VALUES (1,'Type 1') 
INSERT INTO @tableType (ID, sometype) VALUES (2,'Type 2') 
INSERT INTO @tableType (ID, sometype) VALUES (3,'Type 3') 
INSERT INTO @tableType (ID, sometype) VALUES (4,'Type 4') 

INSERT INTO @tableOrders (ID, place, type) values (1 , 1 , 1) -- PLACE A TYPE 1 
INSERT INTO @tableOrders (ID, place, type) values (2 , 1 , 2) -- PLACE A TYPE 2 
INSERT INTO @tableOrders (ID, place, type) values (3 , 2 , 2) -- PLACE B TYPE 2 

Jetzt aufgebaut habe, was ich versuche, die drei Tabellen zu tun, verbinden die folgenden zu erhalten führen

╔═════════╦════════╦═══════╗ 
║ PLACE ║ TYPE ║ COUNT ║ 
╠═════════╬════════╬═══════╣ 
║ PLACE A ║ TYPE 1 ║  1 ║ 
║ PLACE A ║ TYPE 2 ║  1 ║ 
║ PLACE A ║ TYPE 3 ║  0 ║ 
║ PLACE A ║ TYPE 4 ║  0 ║ 
║ PLACE B ║ TYPE 1 ║  0 ║ 
║ PLACE B ║ TYPE 2 ║  1 ║ 
║ PLACE B ║ TYPE 3 ║  0 ║ 
║ PLACE B ║ TYPE 4 ║  0 ║ 
╚═════════╩════════╩═══════╝ 

Also, was ich versuche, die beiden Orte zu tun ist, verknüpfen und die Anzahl der einzelnen Arten zeigen nach den von der @tableorders Tabelle abgerufenen Datensätze.


Meine Anfrage bisher:

SELECT place.someplace, 
     type.sometype, 
     Count(*) AS count 
FROM @tableOrders orders 
     INNER JOIN @tableplace place 
       ON orders.place = place.id 
     INNER JOIN @tabletype type 
       ON place.id = type.id 
GROUP BY someplace, 
      sometype 
ORDER BY someplace, 
      sometype 

Könnte jemand bitte die Logik erklären ich folgen sollte meine gewünschten Ergebnisse zu erzielen? Danke im Voraus!

Antwort

3

Sie möchten eine Querverbindung, um die Zeilen zu generieren. Dann left join (oder korrelierte Unterabfrage) erhält den Wert zu erhalten:

select p.someplace, t.sometype, count(o.id) as count 
from @tableplace p cross join 
    @tabletype t left join 
    @tableOrders o 
    on o.type = t.id and o.place = p.id 
group by p.someplace, t.sometype 
order by p.someplace, t.sometype; 
1

Verwenden CROSS JOIN zur Erzeugung von MxN-Tabelle (die Verbindung aller Orte mit Typen) und dann LEFT JOIN auf Aufträge Tabelle die Zählung für jedes Paar zu bekommen (someplace, sometype):

select tp.someplace, tt.sometype, count(to.id) as count 
from @tablePlace tp 
cross join @tableType tt 
left join @tableOrders to on 
    to.place = tp.id and to.type = tt.id 
group by tp.someplace, tt.sometype 
order by tp.someplace, tt.sometype 
2

Versuchen Sie folgendes:

SELECT place.someplace, [type].sometype , T.cnt 
FROM @tablePlace place 
CROSS join @tabletype [type] 
OUTER APPLY 
( SELECT COUNT(1) cnt 
    FROM @tableOrders tableOrders 
    WHERE tableOrders.place=place.ID AND tableOrders.[type] = [type].ID 
)T 
ORDER BY someplace,sometype 

OUTPUT:

Place A Type 1 1 
Place A Type 2 1 
Place A Type 3 0 
Place A Type 4 0 
Place B Type 1 0 
Place B Type 2 1 
Place B Type 3 0 
Place B Type 4 0 
0

Try this:

SELECT A.someplace,B.sometype,COUNT(C.ID) count_val FROM @tablePlace A CROSS JOIN 
@tableType B LEFT JOIN @tableOrders C ON C.place = A.ID AND C.type = B.ID 
GROUP BY someplace,sometype 

Hoffe, es hilft. :)

Verwandte Themen