Hi I am having a table like this which contains the shift for the people on that day. I need it to be categorized on Shift basis
+------------+----------+-----------------+
| shift_date | name | shift |
+------------+----------+-----------------+
| 2016-07-01 | Swamy | Morning Shift |
| 2016-07-01 | Hitesh | Morning Shift |
| 2016-07-01 | Naresh | Afternoon Shift |
| 2016-07-01 | Rambabu | Afternoon Shift |
| 2016-07-01 | Srinivas | Night Shift |
| 2016-07-01 | Ahmed | Night Shift |
| 2016-07-01 | Naveen | Week Off |
| 2016-07-01 | Karthik | Week Off |
+------------+----------+-----------------+
and I need a query to get result as
+------------+---------+-----------+----------+---------+
| shift_date | Morning | Afternoon | Night | WeekOff |
+------------+---------+-----------+----------+---------+
| 2016-07-01 | Swamy | Naresh | Srinivas | Naveen |
| 2016-07-01 | Hitesh | Rambabu | Ahmed | Karthik |
+------------+---------+-----------+----------+---------+
I have written this query
select
shift_date,
CASE shift
WHEN shift <>'Morning Shift'
then name
end as Morning,
CASE shift
WHEN shift<>'Afternoon Shift'
then name
end as Afternoon,
CASE shift
WHEN shift<>'Night Shift'
then name
end as Night,
CASE shift
WHEN shift<>'Week Off'
then name
end as WeekOff
from roster
where shift_date ='2016-7-1'
order by morning desc ,afternoon desc, night desc ,weekoff desc;
but its getting result as
+------------+---------+-----------+----------+---------+
| shift_date | Morning | Afternoon | Night | WeekOff |
+------------+---------+-----------+----------+---------+
| 2016-07-01 | Swamy | NULL | NULL | NULL |
| 2016-07-01 | Hitesh | NULL | NULL | NULL |
| 2016-07-01 | NULL | Rambabu | NULL | NULL |
| 2016-07-01 | NULL | Naresh | NULL | NULL |
| 2016-07-01 | NULL | NULL | Srinivas | NULL |
| 2016-07-01 | NULL | NULL | Ahmed | NULL |
| 2016-07-01 | NULL | NULL | NULL | Naveen |
| 2016-07-01 | NULL | NULL | NULL | Karthik |
+------------+---------+-----------+----------+---------+
and I need result as
+------------+---------+-----------+----------+---------+
| shift_date | Morning | Afternoon | Night | WeekOff |
+------------+---------+-----------+----------+---------+
| 2016-07-01 | Swamy | Naresh | Srinivas | Naveen |
| 2016-07-01 | Hitesh | Rambabu | Ahmed | Karthik |
+------------+---------+-----------+----------+---------+