Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Comibne two Queries to get results

Author  Topic 

archana23
Yak Posting Veteran

89 Posts

Posted - 2012-10-03 : 14:44:35
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

52326 Posts

Posted - 2012-10-03 : 15:28:07
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
   

- Advertisement -