Solved

sql multiple select statements with different criteria

Posted on 2014-01-29
7
340 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
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL server is using more virtual memory. 5 83
Sql Query with datetime 3 24
SQL 2008 with .NET 4.5.2 4 28
TSQL - How to declare table name 26 28
In this article I will describe the Detach & Attach 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.
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

19 Experts available now in Live!

Get 1:1 Help Now