Solved

sql multiple select statements with different criteria

Posted on 2014-01-29
7
343 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 65

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 65

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

 

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 65

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 65

Expert Comment

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

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Many to one in one row 2 48
SQL parsing XML works but want to do it another way 4 48
Applying Roles in Common Scenarios 3 44
SQL Query Across Multiple Tables - Help 5 48
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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