SQL Server PIVOT - Mehrere Aggregate
-
12-09-2019 - |
Frage
die folgende Ergebnismenge Gegeben:
---------------------------------------------------------
CustomerID Service TransType SubTotal Tax NetTotal
---------------------------------------------------------
106 A CREDIT 12.52 - 12.52
106 A CREDIT 10.07 - 10.07
106 B CREDIT 2.00 - 2.00
106 C REMOTE 5.99 - 5.99
106 C CREDIT 5.99 - 5.99
106 C CREDIT 3.99 0.30 3.69
106 C CREDIT 5.99 0.30 5.69
106 D CREDIT 5.99 - 5.99
---------------------------------------------------------
Beachten Sie, dass NetTotal = SubTotal - Steuer
Bitte helfen Sie mir berechnen Summe (SubTotal), sum (Tax) und sum (NetTotal), zusammen mit lenkt Transtype wie folgt:
--------------------------------------------------------------------------
CustomerID Service Cash Check Credit Remote SubTotal Tax NetTotal
--------------------------------------------------------------------------
106 A 0 0 22.59 0 22.59 0 22.59
106 B 0 0 2.00 0 2.00 0 2.00
106 C 0 0 15.97 5.99 21.96 0.60 21.36
106 D 0 0 5.99 0 5.99 0 5.99
--------------------------------------------------------------------------
Wenn ich nur 1 Spalte habe zusammengefasst werden, um es vorwärts gerade sein würde PIVOT, aber ich bin nicht sicher, wie die drei Aggregate erhalten - für SubTotal, Steuern und NetTotal.
Vielen Dank für Ihre Hilfe!
Lösung
Dies kann ohne PIVOT getan werden:
SELECT
CustomerID
, [Service]
, Cash = SUM(case when TransType='CASH' then SubTotal else 0 end)
, [Check] = SUM(case when TransType='CHECK' then SubTotal else 0 end)
, Credit = SUM(case when TransType='CREDIT' then SubTotal else 0 end)
, [Remote] = SUM(case when TransType='REMOTE' then SubTotal else 0 end)
, SubTotal = SUM(SubTotal)
, Tax = SUM(Tax)
, NetTotal = SUM(NetTotal)
FROM YourTable
GROUP BY CustomerId, [Service]
Mit PIVOT, wird es wesentlich komplexer. Der einfachste Weg, die ich denken kann ist SubTotal, Steuer, und NetTotal in einer anderen Abfrage zu berechnen, und kombiniert dann die Abfragen mit einem Join. Beispiel unten; die Abfrage einfach zu halten, habe ich Bargeld- und Scheck verworfen.
SELECT
a.CustomerId
, a.Service
, Credit = a.Credit
, [Remote] = a.[Remote]
, SubTotal = SUM(b.SubTotal)
, Tax = SUM(b.Tax)
, NetTotal = SUM(b.NetTotal)
FROM (
SELECT
CustomerId
, [Service]
, Credit = SUM(Credit)
, [Remote] = SUM([Remote])
FROM YourTable a
PIVOT
(
SUM(SubTotal) FOR [TransType] IN ([Credit],[Remote])
) pvt
GROUP BY CustomerId, [Service]
) a
INNER JOIN YourTable b
ON a.CustomerID = b.CustomerID
AND a.[Service] = b.[Service]
GROUP BY a.CustomerId, a.[Service], a.Credit, a.[Remote]
Andere Tipps
Haben Sie versucht, so etwas wie das?
DECLARE @Table TABLE(
CustomerID INT,
[Service] VARCHAR(MAX),
TransType VARCHAR(MAX),
SubTotal FLOAT,
Tax FLOAT,
NetTotal FLOAT
)
INSERT INTO @Table (CustomerID,[Service],TransType,SubTotal,Tax,NetTotal)
SELECT 106, 'A', 'CREDIT', 12.52, 0 , 12.52
INSERT INTO @Table (CustomerID,[Service],TransType,SubTotal,Tax,NetTotal)
SELECT 106, 'A', 'CREDIT', 10.07, 0 , 10.07
INSERT INTO @Table (CustomerID,[Service],TransType,SubTotal,Tax,NetTotal)
SELECT 106, 'B', 'CREDIT', 2, 0 , 2
INSERT INTO @Table (CustomerID,[Service],TransType,SubTotal,Tax,NetTotal)
SELECT 106, 'C', 'REMOTE', 5.99, 0 , 5.99
INSERT INTO @Table (CustomerID,[Service],TransType,SubTotal,Tax,NetTotal)
SELECT 106, 'C', 'CREDIT', 5.99, 0 , 5.99
INSERT INTO @Table (CustomerID,[Service],TransType,SubTotal,Tax,NetTotal)
SELECT 106, 'C', 'CREDIT', 3.99, 0.3 , 3.69
INSERT INTO @Table (CustomerID,[Service],TransType,SubTotal,Tax,NetTotal)
SELECT 106, 'C', 'CREDIT', 5.99, 0.3 , 5.69
INSERT INTO @Table (CustomerID,[Service],TransType,SubTotal,Tax,NetTotal)
SELECT 106, 'D', 'CREDIT', 5.99, 0 , 5.99
SELECT Pivots.CustomerID,
Pivots.[Service],
Pivots.Cash,
Pivots.[Check],
Pivots.Credit,
Pivots.[Remote],
Total.SumSubTotal,
Total.SumTax,
Total.SumNetTotal
FROM (
SELECT CustomerID,
[Service],
[Cash] Cash,
[Check] [Check],
[Credit] Credit,
[Remote] [Remote]
FROM (
SELECT CustomerID,
[Service],
TransType,
SubTotal
FROM @Table
) sub
PIVOT
(
SUM(SubTotal)
FOR [TransType] IN ([Cash],[Check],[Credit],[Remote])
) pvt
) Pivots INNER JOIN
(
SELECT CustomerID,
[Service],
SUM(SubTotal) SumSubTotal,
SUM(Tax) SumTax,
SUM(NetTotal) SumNetTotal
FROM @Table
GROUP BY CustomerID,
[Service]
) Total ON Pivots.CustomerID = Total.CustomerID
AND Pivots.[Service] = Total.[Service]