Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
alexander1
Starting Member
1 Post |
Posted - 2013-11-16 : 17:09:38
|
I have looked at the examples of GROUP BY ROLLUP, but my application isa little more complicated.In the SQL below, I do a JOIN that destroys the ROLLUP because the ID fields are NULL in a ROLLUP.Does anybody know how to make the ROLLUP work. Some re-structuring is obviously required. Please see the present results. They are correct, but don't have the ROLLUP.ThxAlexSELECTO.Id,O.Wood_Type_Id,O.Product_Id,O.WeekVolume,O.MonthVolume,C.Est_Volume,O.ToDateVolumeFROMCruise C JOIN(SELECTC.Id,C.Wood_Type_Id,C.Product_Id,SUM(IIf(DATEPART(wk,T.Ticket_Date) = DATEPART(wk,GETDATE()) ,isnull(T.Volume,0),0)) AS WeekVolume,SUM(IIf(DATEPART(mm,T.Ticket_Date) = DATEPART(mm,GETDATE()) ,isnull(T.Volume,0),0)) AS MonthVolume,SUM(isnull(T.Volume,0)) AS ToDateVolume,SUM(CT.Total_Volume) AS ClosedToDateVolume,SUM(CT.Stumpage_Cost) AS ClosedStumpageCost,SUM(CT.Total_Cost) AS ClosedTotalCost,SUM(CT.income) AS ClosedToDateIncomeFROMCruise C LEFT OUTER JOIN Tickets T ON C.Id = T.Tract_Id and C.Product_Id = T.Product_Id LEFT OUTER JOIN Closed_Tracts CT ON C.Id = CT.Tract_Id and C.Product_Id = CT.Product_IdGROUP BY ROLLUP (C.Id, C.Wood_Type_Id, C.Product_Id)) OON C.Id = O.Id and C.Wood_Type_Id = O.Wood_Type_Id and C.Product_Id = O.Product_Id Id Wood_Type_Id Product_Id WeekVolume MonthVolume Est_Volume ToDateVolume3 0 0 0.00 0.00 300.00 40000.004 1 8 0.00 0.00 100.00 19740.005 1 9 0.00 0.00 200.00 0.006 2 5 0.00 0.00 750.00 0.007 2 6 0.00 0.00 600.00 0.008 3 2 0.00 0.00 800.00 0.009 3 3 0.00 0.00 750.00 0.0010 3 3 0.00 0.00 750.00 0.0011 3 3 0.00 0.00 750.00 0.0012 1 9 0.00 0.00 500.00 0.0013 1 8 0.00 0.00 400.00 0.00 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-17 : 11:56:28
|
Add GROUPING function based column in inner query and add OR condition in the INNER JOIN to include that as well.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|