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
 union or pivot

Author  Topic 

bjtexas
Starting Member

1 Post

Posted - 2010-01-14 : 09:48:45
I have a table with fields for location, date, desc, value. There are multiple desc, for each date; multiple dates for each location. For example:
city1,01/01/10,desc1,1000
city1,01/01/10,desc2,5
city1, 01/07/10, desc1,1500
city1, 1/7/10, desc2, 30

I need to be able to select and present the data by desc field, like:
desc1 desc2
1/1 1000 5
1/7 1500 30

I am a new sql programmer and tried union and pivots without success. Any suggestions would be appreciated.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-14 : 09:50:43
[code]select location,date,[desc1],[desc2]
from table
pivot (max(value) for desc in ([desc1],[desc2]))p
[/code]
Go to Top of Page
   

- Advertisement -