Table structures
Table Products :
=============================================
ID | Store | Type | Cost | NetCost |
=============================================
1 |102 | 0 |14.95 | 29.65
2 |103 | 1 |13.95 | 32.65
3 |104 | 2 |12.36 | 42.12
4 |105 | 3 |10.24 | 23.11
5 |106 | 4 |19.57 | 27.16
6 |102 | 0 |57.26 | 26.98
7 |103 | 3 |43.26 | 26.98
8 |104 | 4 |10.26 | 26.98
9 |105 | 1 |9.26 | 26.98
10 |105 | 5 |11.26 | 46.98
Same table separated into two tables by Type
Premium products : Type = (0, 1, 2)
Basic products : Type = (3, 4, 5)
Table Products : Products with Type (0,1,2)
=============================================
ID | Store | Type | Cost | NetCost |
=============================================
1 |102 | 0 |14.95 | 29.65
2 |103 | 1 |13.95 | 32.65
3 |104 | 2 |12.36 | 42.12
6 |102 | 0 |57.26 | 26.98
9 |105 | 1 |9.26 | 26.98
Table Products : Products with Type (3,4,5)
=============================================
ID | Store | Type | Cost | NetCost |
=============================================
4 |105 | 3 |10.24 | 23.11
5 |106 | 4 |19.57 | 27.16
7 |103 | 3 |43.26 | 26.98
8 |104 | 4 |10.26 | 26.98
10 |105 | 5 |11.26 | 46.98
ich summieren möchten Graf, Kosten und NetCost für alle Produkte, die auf Typ basierend by Store gruppiert. Das Ergebnis sollte wie folgt aussehen:Konvertieren Sie mehrere Unterabfragen in einer Join (Join-Tabelle selbst)
Table of totals: (B = Basic, P= Premium)
=================================================================
Store | B Cnt |B Cost | B NetCost | P Cnt | P Cost | P NetCost |
=================================================================
102
103
104
105
106
I unter Verwendung eines Master-Abfrage mit den Unterabfragen unten die oben erreichen kann, gibt es eine weitere effizient verbinden (Verbindungs Tabelle mit sich selbst usw.) kann ich tun das wird die Tabelle (Summen) oben erreichen?
Unterabfragen, die ich in einer Master-Abfrage stellen würde:
SELECT
count(*)
FROM
Products prod_prem
WHERE
(prod_prem.Type = 0 OR prod_prem.Type = 1 OR prod_prem.Type = 2)
SELECT
SUM(Cost),
FROM
Products prod_prem
WHERE
(prod_prem.Type = 0 OR prod_prem.Type = 1 OR prod_prem.Type = 2)
SELECT
SUM(NetCost)
FROM
Products prod_prem
WHERE
(prod_prem.Type = 0 OR prod_prem.Type = 1 OR prod_prem.Type = 2)
SELECT
count(*)
FROM
Products prod_basic
WHERE
(prod_prem.Type = 3 OR prod_prem.Type = 4 OR prod_prem.Type = 5)
SELECT
SUM(Cost),
FROM
Products prod_basic
WHERE
(prod_prem.Type = 3 OR prod_prem.Type = 4 OR prod_prem.Type = 5)
SELECT
SUM(NetCost)
FROM
Products prod_basic
WHERE
(prod_prem.Type = 3 OR prod_prem.Type = 4 OR prod_prem.Type = 5)
Das sieht viel sauberer und verständlicher aus, ich werde es heute später versuchen und Ihre Antwort akzeptieren, wenn es klappt. Gracias. – Fabii