Solved

sql multiple select statements with different criteria

Posted on 2014-01-29
7
338 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
  • 4
  • 3
7 Comments
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
>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
Comment Utility
yes... but I want the results to be in different columns
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:solarissf
Comment Utility
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 65

Accepted Solution

by:
Jim Horn earned 500 total points
Comment Utility
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
Comment Utility
thanks
0
 
LVL 65

Expert Comment

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

10 Experts available now in Live!

Get 1:1 Help Now