Solved

Merging SELECT statements

Posted on 2013-01-08
6
147 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

786 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