I have a table as below (Time sheet)..I want to flatten the table to Flattened_TimeSheet..Can I use group by here? If not could someone help me with the query.
Asked
Active
Viewed 34 times
0
-
1You can go with two approaches either using `case` or `pivot`. [Here is pivot](https://stackoverflow.com/questions/11348562/pivoting-with-sum-function-in-tsql) – Mahesh Sep 28 '18 at 01:49
-
1Column value for 1-C should be 3 instead of 4, right? – Dishant Sep 28 '18 at 01:57
-
yes, it was a typo. thank you – Relativity Sep 28 '18 at 02:57
1 Answers
1
try this:
select ID,
sum(case when type='a' then hour else 0 end) as A,
sum(case when type='b' then hour else 0 end) as B,
sum(case when type='c' then hour else 0 end) as C
from TimeSheet
where type in ('a', 'b', 'c')
group by ID

Esperento57
- 16,521
- 3
- 39
- 45