## group wise sum query

 Author Message Raghunathan Valued Member Group: General Forum Members Points: 57 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 JamesI want to display the above records in below format.Date : 19/Jan/2012 Travel Expense 10 Stay Expens 15 Mis expense 0 Sub-Total 25Date : 20/Jan/2012 Travel Expense 5 Stay Expense 5 Mis Expense 5 Sub-Total 15 Total 40 dwain.c SSC-Forever Group: General Forum Members Points: 44055 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 @ExpensesSELECT 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 @ExpensesCROSS 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 Old Hand Group: General Forum Members Points: 331 Visits: 83 where do you want to display...Report, or UI or Query result?