?
Solved

Merging SELECT statements

Posted on 2013-01-08
6
Medium Priority
?
151 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
Cluskitt earned 2000 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

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…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

765 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