Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


group wise sum query


group wise sum query

Author
Message
Raghunathan
Raghunathan
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: 11
I have a table in the below format.

S.no Travel Expense Stay Expense Mis Expens Date Employee
1 10 15 0 19/Jan/2012 James
2 5 5 5 20/Jan/2012 James


I want to display the above records in below format.




Date : 19/Jan/2012 Travel Expense 10
Stay Expens 15
Mis expense 0
Sub-Total 25


Date : 20/Jan/2012 Travel Expense 5
Stay Expense 5
Mis Expense 5
Sub-Total 15
Total 40
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4289 Visits: 6431
Is something like this close enough for you?


DECLARE @Expenses TABLE
([S.no] INT
,[Travel Expense] MONEY
,[Stay Expense] MONEY
,[Mis Expens] MONEY
,[Date] DATE
,[Employee] VARCHAR(10))

INSERT INTO @Expenses
SELECT 1,10,15,0,'2012-01-19','James' UNION ALL SELECT 2,5,5,5,'2012-01-20','James'

SELECT [Date]
,CASE WHEN Category IS NULL AND [Date] IS NULL THEN 'Total'
WHEN Category IS NULL THEN 'Subtotal'
ELSE Category END
,Expense=SUM(Expense)
FROM @Expenses
CROSS APPLY (
VALUES ('Travel Expense', [Travel Expense])
,('Stay Expense', [Stay Expense])
,('Mis Expens', [Mis Expens])) a(Category, Expense)
GROUP BY Date, Category WITH ROLLUP





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
Suresh-209155
Suresh-209155
SSC Veteran
SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)

Group: General Forum Members
Points: 273 Visits: 83
where do you want to display...Report, or UI or Query result?
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