Solved

Merging SELECT statements

Posted on 2013-01-08
6
144 Views
Last Modified: 2013-01-08
Hi,

There is probably a simple solution to this but I need to resolve very urgently. Unfortunately I'm not too good with SQL!

I have a T-SQL query for SQL 2008

DECLARE @thismonth varchar(7)

SET @thismonth = CONVERT(varchar(4),GETDATE(),120) + '-' + CONVERT(varchar(2),GETDATE(),101)

SELECT [Target] AS [Budget GP] FROM dbo.tblTarget
WHERE [SalesChannel] = 'HR' AND [Period] = @thismonth

SELECT
SUM(ISNULL(CASE WHEN [Trx Type]='credit' THEN (GP * -1) WHEN [Trx Type]='invoice' THEN (GP) END , 0)) AS [Billing] , 

SUM(ISNULL(CASE WHEN [Trx Type]='booking' THEN GP END , 0)) AS [Booking] 

FROM  dbo.tblTransactions 

WHERE [Sales Channel] = 'HR' AND [Doc Date YYYY-MM] = @thismonth

Open in new window

How can I merge the two select statements into one so I see the Budget / Billing / Booking columns in the same result?

Thanks
0
Comment
Question by:gremlin150
  • 3
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
Cluskitt earned 500 total points
ID: 38754843
SELECT Target AS 'Budget GP', SUM(ISNULL(CASE WHEN [Trx Type]='credit' THEN (GP * -1) WHEN [Trx Type]='invoice' THEN (GP) END , 0)) AS 'Billing' , SUM(ISNULL(CASE WHEN [Trx Type]='booking' THEN GP END , 0)) AS 'Booking'
FROM dbo.tblTarget T1
INNER JOIN dbo.tblTransactions T2
ON T1.SalesChannel=T2.SalesChannel AND T1.Period=T2.[Doc Date YYYY-MM]
WHERE T1.SalesChannel='HR'

Something like this? Notice that, because of the INNER join, if there are no matches between the two tables, you won't get results. To get them, you'd need a LEFT, RIGHT or FULL OUTER JOIN.
0
 
LVL 5

Expert Comment

by:RehanYousaf
ID: 38754852
You dont need merge ... what you need is join for two select statements
0
 
LVL 5

Expert Comment

by:RehanYousaf
ID: 38754871
Can you paste the results for both of your select statements?
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 18

Expert Comment

by:Cluskitt
ID: 38754890
One thing I forgot, since you're using SUM for two of the fields, you'll have to add:
GROUP BY Target
0
 

Author Closing Comment

by:gremlin150
ID: 38755199
Thanks all!

Cluskitt's solution worked (once the GROUP BY clause was added). I needed to change the line ON T1.SalesChannel=T2.SalesChannel AND T1.Period=T2.[Doc Date YYYY-MM] to ON T1.[SalesChannel]=T2.[Sales Channel] AND T1.Period = @thismonth AND T2.[Doc Date YYYY-MM] = @thismonth as it was showing all targets and sales info rather than for the current month. But it works so thanks for your help as it got me out of a jam!
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38755226
You should leave the on clause as it was. It was my bad that you should have added:
AND T1.Period=@thismonth
to the WHERE clause (you could also compare to the doc date. Either would be fine but only one is necessary because of the join).
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sql server lock cursor 13 41
SSRS Calculation of two fields not working 7 36
Sql query 34 22
Authentication error 1 0
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now