SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Returning correct tsql results for MTD and YTD
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

parthhetal
Starting Member

Australia
2 Posts

Posted - 03/13/2013 :  17:47:16  Show Profile  Reply with Quote
Hi folks,

i have following select query with where clause which should return current date and current year total for MTD and YTD

SELECT
OES.YR as YEAR
, SUM(OES.SALESAMTF) OVER (PARTITION BY OES.YR) AS YTDTotalSales
, DATENAME(month, DATEADD(month, OES.PERIOD, 0) - 1) AS MONTH
, SUM(OES.SALESAMTF) OVER (PARTITION BY OES.PERIOD) AS MTDTotalsales
, OES1.OE_IDCUST
, OES1.OE_statsid
FROM SAMINC.dbo.OESTATS OES
FULL OUTER JOIN dbo.OESTATS1 OES1 ON OES.LINVCUST = OES1.OE_IDCUST COLLATE Latin1_General_BIN
where OES.YR = YEAR(getdate()) AND OES.PERIOD = MONTH(getdate())

which returns me rows below:

YEAR YTDTotalSales MONTH MTDTotalsales

2013 1512.500 January 575.000


2013 1512.500 March 937.500


the above is fine - what i want to achieve is only the second row from above that current YTD sales and current month to date sales

YEAR YTDTotalSales MONTH MTDTotalsales

2013 1512.500 March 937.500


please advise.



UnemployedInOz
Yak Posting Veteran

Australia
54 Posts

Posted - 03/13/2013 :  20:37:45  Show Profile  Reply with Quote
Some how the OES.Period in DATENAME(month, DATEADD(month, OES.PERIOD, 0) - 1)
is a 1 which cannot be if OES.PERIOD = MONTH(getdate()) in the WHERE.

I suggest you add OES.Period as an output in your select and have a look to see if it is actually returning a 3 or 1 and take it from there.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000