Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Grouping Sets Grand Total and # of Days in Month

$
0
0

My query below is working until it needs to Grand Total on for all Facilities. How would you rewrite this query so that SQL knows to sum all the charges by all the days in the month in the record set? If i add SUM to the DATEPATE portion and then take ReportMonth out of the GROUPING SETS it does not work corectly. Fiscal Month is a string and ReportMonth is the date representation of the Fiscal Month in a date format. Thank you.

SELECT
'Average Daily Revenue' AS Measure
,COALESCE(ac.FiscalMonth,'zGrand Total') AS FiscalPeriod
,COALESCE(ac.Department,'All') AS FacilityName
,COALESCE(d.FacilityId,'0') AS FacilityNumber
,SUM(ac.Chg) / (DATEPART(DAY, DATEADD(MONTH, DATEDIFF(MONTH,-1,ac.ReportMonth),-1))) AS TransAmount --
FROM
#ACharge AS ac
INNER JOIN dbo.Facility AS d
ON ac.FacilityId= d.FacilityId

GROUP BY GROUPING SETS((ac.FiscalMonth,ac.Department,d.FacilityId,ac.ReportMonth),(ac.FiscalMonth,ac.ReportMonth),(ac.Department,d.FacilityId),())



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>