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
 General SQL Server Forums
 New to SQL Server Programming
 Comibne two Queries to get results
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

archana23
Yak Posting Veteran

74 Posts

Posted - 10/03/2012 :  14:44:35  Show Profile  Reply with Quote
Hello,

I am using this query

SELECT *
FROM
(
SELECT InvDate, Location, Inv+obs as Total
FROM InvInfo where (InvDate >= '8/1/2012')
and (InvDate <= '8/31/2012') Group by InvDate ,Location
) d
PIVOT
(
SUM (Total)
FOR Location in ( [Dallas', [Atlanta], [Houston], [Raleigh], [Chicago] )
) p

By using above query getting this results

Date Dallas Atlanta Houston Raleigh Chicago
1 100 50 80 75 120
2 80 60 77 82 110

But In above results i want one more extra column(OBS) , thats gives my Actual results as shown below

Here is the Expecting Results

Date Dallas Atlanta Houston Raleigh Chicago OBS
1 100 50 80 75 120 20
2 80 60 77 82 110 18


This extra columns will get by below query

select sum(OBS) as OBS from InvInfo where (InvDate >= '8/1/2012')
and (InvDate <= '8/31/2012') Group by InvDate

So how to combine these two queries to get Actual results as shown above.

Please anyone Can help me on this?

Thanks

Archana

Archana

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 10/03/2012 :  15:28:07  Show Profile  Reply with Quote
see

http://visakhm.blogspot.com/2012/04/display-total-rows-with-pivotting-in-t.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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.03 seconds. Powered By: Snitz Forums 2000