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


Merging two select statements for MTD and YTD


Merging two select statements for MTD and YTD

Author
Message
parthmeister
parthmeister
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 65
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.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16639 Visits: 17026
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)
Sergiy
Sergiy
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5848 Visits: 11407
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.
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