Solved

Merging SELECT statements

Posted on 2013-01-08
6
145 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

22 Experts available now in Live!

Get 1:1 Help Now