Friday, February 24, 2012

Generate Cross tab temp table

Hi,
I have a table like this:
Year OfficeID Revenue
--
2005 1 13
2005 2 14
2005 3 12
And about 100 0other rows.
What SQL statements can give me the following result:
Year Office1 Office2 Office3 ........
---
2005 13 14 12 ........
TIAhttp://www.aspfaq.com/2462
"Ed Chiu" <EdChiu@.discussions.microsoft.com> wrote in message
news:B1F06E5B-A9C8-4977-B891-862F8979E3A1@.microsoft.com...
> Hi,
> I have a table like this:
> Year OfficeID Revenue
> --
> 2005 1 13
> 2005 2 14
> 2005 3 12
> And about 100 0other rows.
> What SQL statements can give me the following result:
> Year Office1 Office2 Office3 ........
> ---
> 2005 13 14 12 ........
> TIA|||Try,
select
Year,
sum(case when OfficeID = 1 then Revenue else 0 end) as OfficeID1,
sum(case when OfficeID = 2 then Revenue else 0 end) as OfficeID2,
sum(case when OfficeID = 3 then Revenue else 0 end) as OfficeID3
from
t1
group by
Year
go
HOW TO: Rotate a Table in SQL Server
http://support.microsoft.com/defaul...574&Product=sql
AMB
"Ed Chiu" wrote:

> Hi,
> I have a table like this:
> Year OfficeID Revenue
> --
> 2005 1 13
> 2005 2 14
> 2005 3 12
> And about 100 0other rows.
> What SQL statements can give me the following result:
> Year Office1 Office2 Office3 ........
> ---
> 2005 13 14 12 ........
> TIA

No comments:

Post a Comment