- Home /

I've got a final table I've created which I believe I need to use PIVOT on in order to get my desired results, but not sure how to. Any help would be GREATLY appreciated as I'm new to most of this. (Using SQL Server 2005)

A representation of MyTable:

**Person** , **date** , **C1** , **C2**

Person1 , 2009-01-01 , .89 , .39

Person1 , 2009-02-01 , .99 , .85

Person1 , 2009-03-01 , .78 , .98

etc..

Person2 , 2009-01-01 , .80 , .81

Person2 , 2009-02-01 , .75 , .79

etc..

A representation of the results I'd like to get (Each Person's percentages by month(date)):

**Person , JanC1 , JanC2 , FebC1 , FebC2 , MarC1 , MarC2**

Person1 , .89 , .39 , .99 , .85 , .78 , .98 , etc...

Person2 , .80 , .81 , .75 , .79 , etc...

...I initially tried to use Selects with Cases, but without setting Person to a specific one, I don't know how to generate results

Comment

Scot Hauder

**Answer** by Andomar
·
Nov 03, 2009 at 09:33 PM

I'm not sure if you can PIVOT on a date column, but this will work:

```
select
Person,
JanC1 = SUM(case when date = '2009-01-01' then C1 end),
JanC2 = SUM(case when date = '2009-01-01' then C2 end),
FebC1 = SUM(case when date = '2009-02-01' then C1 end),
FebC2 = SUM(case when date = '2009-02-01' then C2 end)
from <YourTable>
group by Person
```

Copyright 2018 Redgate Software. Privacy Policy

- Anonymous
- Sign in
- Create
- Ask a question
- Spaces
- Ask SSC Site Issues (meta-askssc)
- Explore
- Topics
- Questions
- Users
- Badges