Is there a better way to write this SQL and get the same results?
IF OBJECT_ID('tempdb..#TEMPTable_Main') IS NOT NULL
BEGIN
DROP TABLE #TEMPTable_Main
END
CREATE TABLE #TEMPTable_Main
(
My_ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY
, My_Account VARCHAR(10) NOT NULL
, My_Deposit INT NOT NULL
)
INSERT INTO #TEMPTable_Main (My_Account, My_Deposit) VALUES ('Jan', 10)
INSERT INTO #TEMPTable_Main (My_Account, My_Deposit) VALUES ('Jan', 20)
INSERT INTO #TEMPTable_Main (My_Account, My_Deposit) VALUES ('Jan', 30)
INSERT INTO #TEMPTable_Main (My_Account, My_Deposit) VALUES ('Feb', 40)
INSERT INTO #TEMPTable_Main (My_Account, My_Deposit) VALUES ('Feb', 50)
INSERT INTO #TEMPTable_Main (My_Account, My_Deposit) VALUES ('Feb', 60)
INSERT INTO #TEMPTable_Main (My_Account, My_Deposit) VALUES ('Mar', 70)
INSERT INTO #TEMPTable_Main (My_Account, My_Deposit) VALUES ('Mar', 80)
/*
SELECT * FROM #TEMPTable_Main
*/
SELECT
aT1.*
, aCombine.My_Total_Deposit
FROM #TEMPTable_Main aT1
LEFT OUTER JOIN
(
SELECT
aM1.My_ID
, aM1.My_Account
, aS1.My_Total_Deposit
FROM
(
SELECT
My_Account
, MAX(My_ID) AS My_ID
FROM #TEMPTable_Main
GROUP BY My_Account
) aM1
INNER JOIN
(
SELECT
My_Account
, SUM(My_Deposit) AS My_Total_Deposit
FROM #TEMPTable_Main
GROUP BY My_Account
) aS1 ON aS1.My_Account = aM1.My_Account
) aCombine ON aCombine.My_ID = aT1.My_ID
I have attempted using this which is close but would still like the NULLs until the subTotal.
SELECT
aT1.My_ID
, aT1.My_Account
, aT1.My_Deposit
, SUM(aT1.My_Deposit) OVER (PARTITION BY aT1.My_Account ORDER BY aT1.My_ID) AS 'Total'
FROM #TEMPTable_Main aT1
ORDER BY aT1.My_ID
How about this? Is there a better way? Otherwise, can someone give me a pat on the back on this? Hahaha!
SELECT
aT1.My_ID
, aT1.My_Account
, aT1.My_Deposit
, CASE
WHEN aT1.My_ID = MAX(aT1.My_ID) OVER (PARTITION BY aT1.My_Account ORDER BY aT1.My_ID
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) --AS 'Max ID'
THEN SUM(aT1.My_Deposit) OVER (PARTITION BY aT1.My_Account ORDER BY aT1.My_ID)
END AS 'Total'
FROM #TEMPTable_Main aT1
ORDER BY aT1.My_ID
I compared below to yours. This had 49 reads vs 78 that yours had, so that;s a little better. I would need to test it on larger scale to tell you if it's better
select mY_ID, My_Account, My_Deposit,
case when my_Account <> NextAccount then
SUM(My_Deposit) OVER (PARTITION BY My_Account ORDER BY My_ID)
END AS 'Total'
from (
SELECT
aT1.My_ID
, aT1.My_Account
, aT1.My_Deposit
, lead(aT1.My_Account,1,0) over (PARTITION BY aT1.My_Account ORDER BY aT1.My_ID) as 'NextAccount'
FROM #TEMPTable_Main aT1) v
ORDER BY My_ID
Run this, then click on messages tab in results screen
set statistics io on
go
SELECT
aT1.*
, aCombine.My_Total_Deposit
FROM #TEMPTable_Main aT1
LEFT OUTER JOIN
(
SELECT
aM1.My_ID
, aM1.My_Account
, aS1.My_Total_Deposit
FROM
(
SELECT
My_Account
, MAX(My_ID) AS My_ID
FROM #TEMPTable_Main
GROUP BY My_Account
) aM1
INNER JOIN
(
SELECT
My_Account
, SUM(My_Deposit) AS My_Total_Deposit
FROM #TEMPTable_Main
GROUP BY My_Account
) aS1 ON aS1.My_Account = aM1.My_Account
) aCombine ON aCombine.My_ID = aT1.My_ID
IF OBJECT_ID('tempdb..#TEMPTable_Main') IS NOT NULL
BEGIN
DROP TABLE #TEMPTable_Main
END
CREATE TABLE #TEMPTable_Main
(
My_ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY
, My_Account VARCHAR(10) NOT NULL
, My_Deposit INT NOT NULL
)
INSERT INTO #TEMPTable_Main (My_Account, My_Deposit) VALUES ('Jan', 10)
INSERT INTO #TEMPTable_Main (My_Account, My_Deposit) VALUES ('Jan', 20)
INSERT INTO #TEMPTable_Main (My_Account, My_Deposit) VALUES ('Jan', 30)
INSERT INTO #TEMPTable_Main (My_Account, My_Deposit) VALUES ('Feb', 40)
INSERT INTO #TEMPTable_Main (My_Account, My_Deposit) VALUES ('Feb', 50)
INSERT INTO #TEMPTable_Main (My_Account, My_Deposit) VALUES ('Feb', 60)
INSERT INTO #TEMPTable_Main (My_Account, My_Deposit) VALUES ('Mar', 70)
INSERT INTO #TEMPTable_Main (My_Account, My_Deposit) VALUES ('Mar', 80);
with cte as
(
SELECT
sum(My_Deposit) as My_Total_Deposit
,My_Account
from #TEMPTable_Main
group by My_Account
)
,cte2 as
(
select t.*,c.My_Total_Deposit from #TEMPTable_Main t
left outer join cte c
on t.My_Account = c.My_Account
and t.My_ID in (select max(My_ID) from #TEMPTable_Main
group by My_Account)
)
select * from cte2