Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

TSQL Subtotalling Expand / Collapse
Author
Message
Posted Friday, January 18, 2013 8:34 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, February 18, 2013 7:37 AM
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!
Post #1408946
Posted Friday, January 18, 2013 8:52 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 12:25 PM
Points: 5,174, Visits: 12,028
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.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1408956
Posted Friday, January 18, 2013 8:56 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 3:29 AM
Points: 1,678, Visits: 19,554

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]);



____________________________________________________

How to get the best help on a forum

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

Never approach a goat from the front, a horse from the rear, or a fool from any direction.
Post #1408957
Posted Friday, January 18, 2013 9:07 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, February 18, 2013 7:37 AM
Points: 3, Visits: 4
Thank you for this. I will test it on my system to verify that I am getting the desired results.
Post #1408962
Posted Friday, January 18, 2013 9:13 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, February 18, 2013 7:37 AM
Points: 3, Visits: 4
Your solution worked perfectly! Thank you!
Post #1408965
Posted Sunday, January 20, 2013 6:05 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 10:53 PM
Points: 3,421, Visits: 5,359
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!
Post #1409333
Posted Monday, January 21, 2013 1:46 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 3:29 AM
Points: 1,678, Visits: 19,554
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.


____________________________________________________

How to get the best help on a forum

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

Never approach a goat from the front, a horse from the rear, or a fool from any direction.
Post #1409412
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse