Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Merging two select statements for MTD and YTD Expand / Collapse
Author
Message
Posted Tuesday, March 12, 2013 5:07 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, December 9, 2013 9:42 PM
Points: 6, Visits: 61
Hi Guys,

i have two select statements that i want to merge in one with union.

the first query should return YTDSALES (Year to date sales) and the second query should retunr MTDSALES (Month to date sales).

the below union is working fine but i just want extract one value for YTD and one value for MTD so i want to alias the first query with YTDSALES and second query with MTDSALES so i can only extract two values from one single query.

SELECT SAMINC.dbo.OESTATS.YR as YEAR, SUM(SAMINC.dbo.OESTATS.SALESAMTF) AS TotalSales, dbo.OESTATS1.oe_IDCUST, dbo.OESTATS1.OE_statsid
FROM SAMINC.dbo.OESTATS FULL OUTER JOIN
dbo.OESTATS1 ON SAMINC.dbo.OESTATS.LINVCUST = dbo.OESTATS1.oe_IDCUST COLLATE Latin1_General_BIN
where SAMINC.dbo.OESTATS.YR = YEAR(getdate())
GROUP BY SAMINC.dbo.OESTATS.YR, dbo.OESTATS1.oe_IDCUST, dbo.OESTATS1.OE_statsid

UNION

SELECT SAMINC.dbo.OESTATS.YR as YEAR,SUM(SAMINC.dbo.OESTATS.SALESAMTF) AS totalsales, DATENAME(month, DATEADD(month, SAMINC.dbo.OESTATS.PERIOD, 0) - 1) AS MONTH,dbo.OESTATS1.OE_statsid
FROM SAMINC.dbo.OESTATS FULL OUTER JOIN
dbo.OESTATS1 ON SAMINC.dbo.OESTATS.LINVCUST = dbo.OESTATS1.oe_IDCUST COLLATE Latin1_General_BIN
where SAMINC.dbo.OESTATS.PERIOD = MONTH(getdate())
GROUP BY SAMINC.dbo.OESTATS.YR, SAMINC.dbo.OESTATS.PERIOD, dbo.OESTATS1.OE_statsid

please advise.
Post #1430148
Posted Wednesday, March 13, 2013 7:55 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:34 PM
Points: 13,481, Visits: 12,342
It is a little hard to figure out what you are trying to do here but can you just a new column to your 2 queries? One would be 'YTD Sales' and the other 'MTD Sales'

Something like this?

SELECT SAMINC.dbo.OESTATS.YR AS YEAR
,SUM(SAMINC.dbo.OESTATS.SALESAMTF) AS TotalSales
,dbo.OESTATS1.oe_IDCUST
,dbo.OESTATS1.OE_statsid
, 'YTDSales' as SummaryType
FROM SAMINC.dbo.OESTATS
FULL JOIN dbo.OESTATS1 ON SAMINC.dbo.OESTATS.LINVCUST = dbo.OESTATS1.oe_IDCUST COLLATE Latin1_General_BIN
WHERE SAMINC.dbo.OESTATS.YR = YEAR(getdate())
GROUP BY SAMINC.dbo.OESTATS.YR
,dbo.OESTATS1.oe_IDCUST
,dbo.OESTATS1.OE_statsid

UNION

SELECT SAMINC.dbo.OESTATS.YR AS YEAR
,SUM(SAMINC.dbo.OESTATS.SALESAMTF) AS totalsales
,DATENAME(month, DATEADD(month, SAMINC.dbo.OESTATS.PERIOD, 0) - 1) AS MONTH
,dbo.OESTATS1.OE_statsid
, 'MTDSales'
FROM SAMINC.dbo.OESTATS
FULL JOIN dbo.OESTATS1 ON SAMINC.dbo.OESTATS.LINVCUST = dbo.OESTATS1.oe_IDCUST COLLATE Latin1_General_BIN
WHERE SAMINC.dbo.OESTATS.PERIOD = MONTH(getdate())
GROUP BY SAMINC.dbo.OESTATS.YR
,SAMINC.dbo.OESTATS.PERIOD
,dbo.OESTATS1.OE_statsid



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1430397
Posted Thursday, March 14, 2013 5:26 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 11:10 PM
Points: 4,576, Visits: 8,351
I guess a query WITH ROLLUP would allow avoiding double-run.

Then a row with grouping on YEAR column would contain aggregated values from all rows with grouping on month.
Post #1431302
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse