I have a task
table with following records:
id | client_id | user_id | designation_id | total_hours
-----------------------------------------------------------------
1 | 14 | 134 | 1 | 4
2 | 14 | 135 | 2 | 1
3 | 15 | 136 | 3 | 3
4 | 16 | 137 | 4 | 4.5
And designation
table:
id | title
------------------------
1 | Cloud Analyst
2 | Data Analyst
3 | Data QA Analyst
4 | Project Manager
.
.
Designation records are dynamic and so I don't want to hard-code them.
I am trying to get following output:
client | user | Cloud Analyst | Data Analyst | Data QA Analyst | Project Manager
-------------------------------------------------------------------------------------------
14 | 134 | 4 | | |
14 | 135 | | 1 | |
15 | 136 | | | 3 |
16 | 137 | | | | 4.5
In words:
- list all available designations (from designation table) as
columns
in addition toclient_id
, anddesignation_id
columns fromtask
table. total_hours
values should be placed under respectivedesignation
columns, .i.e, if user iscloud analyst
(by designation_id) hishours
value should be undercloud analyst
column.
Here's how I tried to do:
SELECT t.client_id, t.user_id,
(case
when d.id = t.designation_id then t.total_hours
else ''
end as d.title)
FROM task t
INNER JOIN designations d
on d.id = t.designation_id
ORDER BY client_id ASC
How can I achieve the output?