Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sql multiple select statements with different criteria

Posted on 2014-01-29
7
Medium Priority
?
346 Views
Last Modified: 2014-01-29
I feel like I am making this more complex than it has to be.

I have a main select statement
SELECT *
  FROM [GSS].[dbo].[LedgerDB]
  inner join dbo.AccountsDB on dbo.AccountsDB.Account_ID = dbo.LedgerDB.Account_ID
  where dbo.AccountsDB.Client_ID = 'xxxxxx'
  and Journal_Type = 'NLV'
  --and (Journal_Date = '20140128' or Journal_Date = '20131231')
  order by Journal_Date desc

Open in new window


now all I want to do is have a results statement, that says
from the ABOVE CODE,
select [specific column here] where Journal_Date = '20140128',
select [specific column here] where Journal_Date = '20131231'


I want the results to look like if different columns
col1 = amountJan28, col2 = amount dec31

I accomplished it with the below but I think its very convoluted, is there an easier way?

thanks


SELECT
	a1.Account_Description,
	a1.Journal_Amount,
	a2.Journal_Amount
FROM

	(SELECT 
		dbo.LedgerDB.Account_ID,
		dbo.AccountsDB.Account_Description,
		dbo.LedgerDB.Journal_Date,
		dbo.LedgerDB.Journal_Type,
		dbo.LedgerDB.Journal_Amount,
		dbo.LedgerDB.Currency_Code
	FROM [GSS].[dbo].[LedgerDB]
	inner join dbo.AccountsDB on dbo.AccountsDB.Account_ID = dbo.LedgerDB.Account_ID
	where dbo.AccountsDB.Client_ID = '418BB7B8-0180-4A5A-99F6-CE52E60E694F'
	and Journal_Type = 'NLV'
	and Journal_Date = '20131231') a1
	
	INNER JOIN
	
	(SELECT 
		dbo.LedgerDB.Account_ID,
		dbo.AccountsDB.Account_Description,
		dbo.LedgerDB.Journal_Date,
		dbo.LedgerDB.Journal_Type,
		dbo.LedgerDB.Journal_Amount,
		dbo.LedgerDB.Currency_Code
	FROM [GSS].[dbo].[LedgerDB]
	inner join dbo.AccountsDB on dbo.AccountsDB.Account_ID = dbo.LedgerDB.Account_ID
	where dbo.AccountsDB.Client_ID = '418BB7B8-0180-4A5A-99F6-CE52E60E694F'
	and Journal_Type = 'NLV'
	and Journal_Date = '20140128') a2
	
	ON a1.Account_ID = a2.Account_ID

Open in new window

0
Comment
Question by:solarissf
[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
  • 4
  • 3
7 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39818045
>select [specific column here] where Journal_Date = '20140128',
>select [specific column here] where Journal_Date = '20131231'

Is your question that you want to return a set that has Journal_Date of either of the two values?

{your query}
WHERE Journal_Date = '20140128' OR Journal_Date = '20131231'
.. or ..
WHERE Journal_Date IN ('20140128', '20131231')
0
 

Author Comment

by:solarissf
ID: 39818059
yes... but I want the results to be in different columns
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39818077
Just for kicks and giggles, before we get knee-deep in T-SQL, please give us a 'before and after' data mockup of what you're trying to pull off here.

Specifically, would the return set only have one row, where the 'results..different columns' come from both '20140128' and '20131231'?
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 

Author Comment

by:solarissf
ID: 39818095
yes the return set would have only 1 row, and yes.

before data looks like
journalDate              journalamount
20140128                    50
20141231                    25


want results set to be 1 row
col1        col2
50            25
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 39818117
In that case, with simple T-SQL that you can modify to meet your needs,
   Use UNION ALL instead of a JOIN
   Change your columns so that they are all the same, but only populate the relevant columns, with 0 for all the non-relevant columns
   Then throw the whole thing in a subquery, and sum.

SELECT SUM(a.Col1) as Col1, SUM(a.Col2) as Col2
FROM (
   SELECT journalamount as Col1, 0 as Col2
   FROM YourTable
   WHERE journalDate = 20140128    
   UNION ALL
   SELECT 0 as Col1, journalamount as Col2
   FROM YourTable
   WHERE journalDate = 20141231) a
0
 

Author Comment

by:solarissf
ID: 39818151
thanks
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39818198
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 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