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!