Solved

sql multiple select statements with different criteria

Posted on 2014-01-29
7
344 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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 500 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

623 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