SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Combining two queries
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

andypgill
Starting Member

34 Posts

Posted - 08/28/2013 :  17:09:11  Show Profile  Reply with Quote
Hi

I have two queries in SQL linked to excel.

Report 1 shows

Contract A E N P

Income 5000 6000 8000 4000
Costs 4000 7000 6000 3000

Report2 shows

Contract A B E

CustomerA 500 200 400
CustomerB 450 100 200


Is it possible to combine them so I get


Contract A B E N P

Income 5000 6000 8000 4000
Costs 4000 7000 6000 3000

CustomerA 500 200 400
CustomerB 450 100 200

Thanks


Sent from my iPad

James K
Flowing Fount of Yak Knowledge

3724 Posts

Posted - 08/28/2013 :  17:20:58  Show Profile  Reply with Quote
Can you try using a UNION like shown below:
SELECT contract, A, NULL AS B, E, N, P FROM Report1Table
UNION ALL
SELECT contract, A, B, E, NULL, NULL FROM Report2Table
If you have specific ordering needs that would need to be added in.
Go to Top of Page

andypgill
Starting Member

34 Posts

Posted - 08/29/2013 :  02:48:16  Show Profile  Reply with Quote
quote:
Originally posted by James K

Can you try using a UNION like shown below:
SELECT contract, A, NULL AS B, E, N, P FROM Report1Table
UNION ALL
SELECT contract, A, B, E, NULL, NULL FROM Report2Table
If you have specific ordering needs that would need to be added in.



Hi

Thanks for the reply, however to do that wouldn't I need to know all the data (of which in reality there is lots)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 08/29/2013 :  03:16:18  Show Profile  Reply with Quote
quote:
Originally posted by andypgill

quote:
Originally posted by James K

Can you try using a UNION like shown below:
SELECT contract, A, NULL AS B, E, N, P FROM Report1Table
UNION ALL
SELECT contract, A, B, E, NULL, NULL FROM Report2Table
If you have specific ordering needs that would need to be added in.



Hi

Thanks for the reply, however to do that wouldn't I need to know all the data (of which in reality there is lots)


You need to know only about columns involved not on actual data. also all tables should have equal number of columns with corresponding types equal.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

VeeranjaneyuluAnnapureddy
Posting Yak Master

India
169 Posts

Posted - 08/29/2013 :  05:13:07  Show Profile  Reply with Quote
SELECT Contract,A,Null AS 'B',E,N,P FROM ReportTable1
UNION all
SELECT Contract,A,B,E,Null AS 'N',Null AS 'P' FROM ReportTable2

veeranjaneyulu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.1 seconds. Powered By: Snitz Forums 2000