I want all table rows and whenever the value in the bank field changes to a new row with a subtotal of the fields: value1, value2, value3
I want to do the following with an SQL query:
Create Table #MyTable (
bank nvarchar(255),
account nvarchar(255),
value1 decimal(18,2),
value2 decimal(18,2),
value3 decimal(18,2),
);
INSERT INTO #MyTable
(bank, account, value1, value2, value3)
VALUES
('VR-Bank', 'Kto-A', 1, 2, 3),
('VR-Bank', 'Kto-B', 3, 2, 1),
('VR-Bank', 'Kto-C', 4, 4, 5),
('NOSPA', 'Kto-X', 1, 1, 1),
('NOSPA', 'Kto-Y', 4, 3, 2);
I would like the sum without previous summation because these values already exist
Select
bank,
account,
SUM(value1), -- I would like this without SUM()
SUM(value2),
SUM(value3)
from #MyTable
GROUP BY
GROUPING SETS ((bank), (account)) -- Without summing up this does not work
-- NULL Kto-A 1.00 2.00 3.00
-- NULL Kto-B 3.00 2.00 1.00
-- NULL Kto-C 4.00 4.00 5.00
-- NULL Kto-X 1.00 1.00 1.00
-- NULL Kto-Y 4.00 3.00 2.00
-- NOSPA NULL 5.00 4.00 3.00
-- VR-Bank NULL 8.00 8.00 9.00
I would like to achieve the following:
-- ======================================
-- VR-Bank Kto-A 1.00 2.00 3.00 -- here also the value for the bank
-- VR-Bank Kto-B 3.00 2.00 1.00
-- VR-Bank Kto-C 4.00 4.00 5.00
-- VR-Bank NULL 8.00 8.00 9.00 -- here the SUM
-- NOSPA Kto-X 1.00 1.00 1.00
-- NOSPA Kto-Y 4.00 3.00 2.00
-- NOSPA NULL 5.00 4.00 3.00 -- SUM for the next bank
Drop Table #MyTable;
I hope someone can help me.