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


Data roll up


Data roll up

Author
Message
sqlenthu 89358
sqlenthu 89358
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1345 Visits: 299
Hi all,
I have one table which contains data like this:
Col1,Col2,Col3
A,before,100
A,after,10
A,after,80
B,before,280
B,before,20
B,after,250
C,before,150
C,after,220


Now i want my output as
Col1,Col2,Amt
A,before,100
A,after,90
A,Diff,10
B,before,300
B,after,250
B,Diff,50
C,before,150
C,after,220
C,Diff,-70

The before, after have to be rolled up and diff has to be calculated based on the respective rolled up before and after amount. Any efficient way ?
Thom A
Thom A
SSC-Forever
SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)

Group: General Forum Members
Points: 48626 Visits: 16183
You know by now how to post a T-SQL question. Please supply DDL and Consumable Sample Data.


Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does :-P

Please always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].
Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community
Bert-701015
Bert-701015
Mr or Mrs. 500
Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)

Group: General Forum Members
Points: 563 Visits: 880
Try this out:

DECLARE @Table table (
Col1 char(1),
Col2 varchar(6),
Col3 smallint
)
;
insert @Table
values
('A','before',100),
('A','after',10),
('A','after',80),
('B','before',280),
('B','before',20),
('B','after',250),
('C','before',150),
('C','after',220)
;
With groups (Col1, Col2, Col3) as (
select Col1, Col2, sum(Col3) AS Col3
from @Table
group by Col1, Col2
)
select Col1, Col2, Amt
from (
select '1' as Seq, Col1, Col2, Col3 as Amt from Groups where Col2 = 'before'
union all
select '2' as Seq, Col1, Col2, Col3 as Amt from Groups where Col2 = 'after'
union all
select '3' as Seq, B.Col1, 'Diff' as Col2, B.Col3 - A.Col3 as Amt
from Groups B
inner join Groups A on B.Col1 = A.Col1
where B.Col2 = 'before'
and A.Col2 = 'after'
) r
order by Col1, Seq
Jacob Wilkins
Jacob Wilkins
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16764 Visits: 12392
Using the sample data DDL Bert generously supplied, here's an alternative that reads the base table 1 time instead of 6:

SELECT Col1, 
Col2=CASE WHEN GROUPING(Col2)=1 THEN 'Diff' ELSE Col2 END,
Col3=CASE WHEN GROUPING(Col2)=1 THEN SUM(CASE WHEN Col2='before' THEN Col3 ELSE 0 END)-SUM(CASE WHEN Col2='after' THEN Col3 ELSE 0 END) ELSE SUM(Col3) END
FROM @Table
GROUP BY GROUPING SETS ((Col1,Col2),(Col1))
ORDER BY Col1,CASE WHEN Col2='before' THEN 0 WHEN Col2='After' THEN 1 ELSE 2 END;




Cheers!
drew.allen
drew.allen
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40015 Visits: 14483
This gives the expected results and only requires one scan of the table.

SELECT col1, CASE WHEN GROUPING(col2) = 1 THEN 'diff' ELSE col2 END AS col2, CASE WHEN col2 = 'after' THEN -1 ELSE 1 END * SUM(CASE WHEN col2 = 'before' THEN col3 ELSE -col3 END)
FROM @Table
GROUP BY GROUPING SETS( (col1, col2), (col1) )
ORDER BY col1, GROUPING(col2), col2 DESC


Drew

Edit: Removed extraneous GROUPING(col1) from the ORDER BY clause.

J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
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