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


Query that Returns Month to Date and YTD in Same Record


Query that Returns Month to Date and YTD in Same Record

Author
Message
cdun2
cdun2
SSC-Addicted
SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)

Group: General Forum Members
Points: 481 Visits: 291
Hello,
I have a query that returns a daily revenue figure. The query is as follows:

SELECT top 1000
ds.AcctCode,
ds.TxnDate,
SUM(isnull(ds.FuelFee,0)) + SUM(isnull(ds.CashFee,0)) + SUM(isnull(ds.ScFee,0)) AS TotalDailyRevenue,
--"MTD" = ?,
--"YDT" = ?,
ps.TC,
CASE
WHEN ps.Proj = 100 THEN 'New Account'
WHEN ps.Proj = 200 THEN 'Current Account'
END AS ProjStatus,
ps.FSR,
ps.SubmitRep1

FROM
TxnRptg.dbo.tbl_DailySummary ds
INNER JOIN SalesData.dbo.tbl_CYProcessedSales ps
ON ds.AcctCode = ps.Acct

WHERE
MONTH(ds.TxnDate) = 1
AND
Proj IN (100,200)
AND TC = 'HV'

GROUP BY
ds.AcctCode, ds.TxnDate, ps.TC, ps.Proj, ps.FSR, ps.SubmitRep1

ORDER BY
ds.AcctCode, ds.TxnDate

--*********************************

TxnDate represents a single day of the month. How can I include MTD so that the dates for the revenue total are from DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) (beginning of current month) to TxnDate, and YTD so that the revenue totals are from DATEADD(yy, DATEDIFF(yy,0,getdate()), 0) (beginning of the current year) to TxnDate?

Thank you for your help!

CSDunn



antonio.collins
antonio.collins
SSC-Addicted
SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)

Group: General Forum Members
Points: 454 Visits: 921

SELECT top 1000
ds.AcctCode,
ds.TxnDate,
SUM(isnull(ds.FuelFee,0)) + SUM(isnull(ds.CashFee,0)) + SUM(isnull(ds.ScFee,0)) AS TotalDailyRevenue,
--"MTD" = ?,
--"YDT" = ?,
ps.TC,
CASE
WHEN ps.Proj = 100 THEN 'New Account'
WHEN ps.Proj = 200 THEN 'Current Account'
END AS ProjStatus,
ps.FSR,
ps.SubmitRep1

FROM
TxnRptg.dbo.tbl_DailySummary ds
INNER JOIN SalesData.dbo.tbl_CYProcessedSales ps
ON ds.AcctCode = ps.Acct

WHERE
MONTH(ds.TxnDate) = 1
AND
Proj IN (100,200)
AND TC = 'HV'

GROUP BY
ds.AcctCode, ds.TxnDate, ps.TC, ps.Proj, ps.FSR, ps.SubmitRep1

ORDER BY
ds.AcctCode, ds.TxnDate


try this.

SELECT
ds.AcctCode,
sum( case when ds.TxDate >= DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)
then (isnull(ds.FuelFee,0)) + SUM(isnull(ds.CashFee,0)) + SUM(isnull(ds.ScFee,0))
else 0 ) AS MTD,
SUM(isnull(ds.FuelFee,0) + isnull(ds.CashFee,0) + isnull(ds.ScFee,0)) AS YTD,
ps.TC,
CASE
WHEN ps.Proj = 100 THEN 'New Account'
WHEN ps.Proj = 200 THEN 'Current Account'
END AS ProjStatus,
ps.FSR,
ps.SubmitRep1

FROM
TxnRptg.dbo.tbl_DailySummary ds
INNER JOIN SalesData.dbo.tbl_CYProcessedSales ps
ON ds.AcctCode = ps.Acct
WHERE
Proj IN (100,200)
AND TC = 'HV'
AND ds.TxnDate >= DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)

GROUP BY
ds.AcctCode, ps.TC, ps.Proj, ps.FSR, ps.SubmitRep1

ORDER BY
ds.AcctCode


GSquared
GSquared
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14375 Visits: 9729
You'll need to run them separately, then combine them in a final query.

Perhaps something like:


;with
Daily (DAcctCode, TotalDailyRevenue, DDay, DMonth, DYear) as
(select AcctCode,
isnull(sum(FuelFee), 0)
+ isnull(sum(CashFee), 0)
+ isnull(sum(ScFee), 0),
datepart(day, TxnDate),
datepart(month, TxnDate)
datepart(year, TxnDate)
from TxnRptg.dbo.tbl_DailySummary
group by AcctCode,
datepart(day, TxnDate),
datepart(month, TxnDate)
datepart(year, TxnDate)),
Monthly (MAcctCode, TotalMonthlyRevenue, MMonth, MYear) as
(select AcctCode,
isnull(sum(FuelFee), 0)
+ isnull(sum(CashFee), 0)
+ isnull(sum(ScFee), 0),
datepart(month, TxnDate)
datepart(year, TxnDate)
from TxnRptg.dbo.tbl_DailySummary
group by AcctCode,
datepart(month, TxnDate)
datepart(year, TxnDate)),
Yearly (YAcctCode, TotalYearlyRevenue, YYear) as
(select AcctCode,
isnull(sum(FuelFee), 0)
+ isnull(sum(CashFee), 0)
+ isnull(sum(ScFee), 0),
datepart(year, TxnDate)
from TxnRptg.dbo.tbl_DailySummary
group by AcctCode,
datepart(year, TxnDate))
select DAcctCode as AcctCode, TotalDailyRevenue, TotalMonthlyRevenue,
TotalYearlyRevenue
from Daily
inner join Monthly
on DAcctCode = MAcctCode
and DMonth = MMonth
and DYear = MYear
inner join Yearly
on DAcctCode = YAcctCode
and MYear = YYear
and DYear = YYear



Add in other tables for other columns to the Join clause.

Does that give you what you need? It'll take at least three table scans, but it shouldn't be too bad on performance.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
cdun2
cdun2
SSC-Addicted
SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)

Group: General Forum Members
Points: 481 Visits: 291
with the CASE nested in the SUM, I got this:

Msg 130, Level 15, State 1, Line 5
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.



antonio.collins
antonio.collins
SSC-Addicted
SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)SSC-Addicted (454 reputation)

Group: General Forum Members
Points: 454 Visits: 921
cdun2 (2/4/2008)
with the CASE nested in the SUM, I got this:

Msg 130, Level 15, State 1, Line 5
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.


sorry, i was trying to simplify sum(a)+sum(b)+sum(c) to sum(a+b+c). this should work.

sum( case when ds.TxDate >= DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)
then isnull(ds.FuelFee,0) + isnull(ds.CashFee,0) + isnull(ds.ScFee,0)
else 0 end ) AS MTD,


cdun2
cdun2
SSC-Addicted
SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)SSC-Addicted (481 reputation)

Group: General Forum Members
Points: 481 Visits: 291
It looks like this might work.

Thanks again for your help!



ricardo puente
ricardo puente
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 29
Very good thanks!
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