SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


TSQL Subtotalling


TSQL Subtotalling

Author
Message
gates 23131
gates 23131
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 4
Please forgive my ignorance if this is a simple fix but I am having a brain cramp. The goal: derive new records with subtotals of a recordset.
Source recordset:
index, workcode, value
1, CL, 5
2, CL, 10
3, CL, 10
4, LI, 10
5, LI, 10
6, ME, 5
7, ME, 5
8, CL, 10
9, CL, 10
10, CL, 5
11, ME, 10
12, ME, 10

Output:
1, CL, 25
4, LI, 20
6, ME, 10
8, CL, 25
11, ME, 20

Notice that each break of the code is what triggers a new output record. Any assistance on TSQL code for accomplishing this would be greatly appreciated.
Thanks in advance!
Phil Parkin
Phil Parkin
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19610 Visits: 20462
Oops, bad code deleted!


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Mark Cowne
Mark Cowne
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3227 Visits: 24128
Using the technique here http://www.sqlservercentral.com/articles/T-SQL/71550/


WITH SourceRecordset AS (
SELECT [index], workcode, value
FROM (
VALUES(1, 'CL', 5),
(2, 'CL', 10),
(3, 'CL', 10),
(4, 'LI', 10),
(5, 'LI', 10),
(6, 'ME', 5),
(7, 'ME', 5),
(8, 'CL', 10),
(9, 'CL', 10),
(10, 'CL', 5),
(11, 'ME', 10),
(12, 'ME', 10)
) d([index], workcode, value)
),
Grouped AS (
SELECT [index], workcode, value,
ROW_NUMBER() OVER(ORDER BY [index])-
ROW_NUMBER() OVER(PARTITION BY workcode ORDER BY [index]) AS rnDiff
FROM SourceRecordset)
SELECT MIN([index]) AS [index],
workcode,
SUM(value) AS value
FROM Grouped
GROUP BY workcode,rnDiff
ORDER BY MIN([index]);



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




gates 23131
gates 23131
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 4
Thank you for this. I will test it on my system to verify that I am getting the desired results.
gates 23131
gates 23131
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 4
Your solution worked perfectly! Thank you!
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)

Group: General Forum Members
Points: 7707 Visits: 6431
Mark - I'm not sure why you used two window functions:


WITH SourceRecordset AS (
SELECT [index], workcode, value
FROM (
VALUES(1, 'CL', 5),
(2, 'CL', 10),
(3, 'CL', 10),
(4, 'LI', 10),
(5, 'LI', 10),
(6, 'ME', 5),
(7, 'ME', 5),
(8, 'CL', 10),
(9, 'CL', 10),
(10, 'CL', 5),
(11, 'ME', 10),
(12, 'ME', 10)
) d([index], workcode, value)
)
SELECT [index]=MIN([index]), workcode=MAX(workcode), value=SUM(value)
FROM (
SELECT [index], workcode, value
,rn=[index]-ROW_NUMBER() OVER (PARTITION BY workcode ORDER BY [index])
FROM SourceRecordset) a
GROUP BY rn
ORDER BY [index]





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Mark Cowne
Mark Cowne
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3227 Visits: 24128
dwain.c (1/20/2013)
Mark - I'm not sure why you used two window functions:


WITH SourceRecordset AS (
SELECT [index], workcode, value
FROM (
VALUES(1, 'CL', 5),
(2, 'CL', 10),
(3, 'CL', 10),
(4, 'LI', 10),
(5, 'LI', 10),
(6, 'ME', 5),
(7, 'ME', 5),
(8, 'CL', 10),
(9, 'CL', 10),
(10, 'CL', 5),
(11, 'ME', 10),
(12, 'ME', 10)
) d([index], workcode, value)
)
SELECT [index]=MIN([index]), workcode=MAX(workcode), value=SUM(value)
FROM (
SELECT [index], workcode, value
,rn=[index]-ROW_NUMBER() OVER (PARTITION BY workcode ORDER BY [index])
FROM SourceRecordset) a
GROUP BY rn
ORDER BY [index]




Sure, but that requires [index] to be contiguous- it wasn't clear from OP whether than was the case.

____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search