I have a database table with records like the picture:
So each record contains columns related to points of the project lifecycle. I need to categorize those columns in order to know how many columns are ‘completed’ or ‘In Progress’ or ‘Not started’ I want the output to be organized in a simple table.
Advertisement
Answer
Something like this:
DROP TABLE IF EXISTS projects; CREATE TABLE projects(id int primary key, column1 varchar(20),column2 varchar(20),column3 varchar(20)); INSERT INTO projects VALUES( 1,'Completed','Not Started','Completed'); INSERT INTO projects VALUES( 2,'Completed','Not Started','Completed'); INSERT INTO projects VALUES( 3,'Completed','Not Started','In Progress'); select id, count(CASE WHEN left(c,9)='Completed' THEN 1 END) as 'Competed', count(CASE WHEN left(c,11)='In Progress' THEN 1 END) as 'In Progress', count(CASE WHEN left(c,11)='Not Started' THEN 1 END) as 'Not Started' from ( SELECT id,Column1 as C from projects union all SELECT id,Column2 from projects union all SELECT id,Column3 from projects ) x group by id;
output:
id Competed In Progress Not Started ----------- ----------- ----------- ----------- 1 2 0 1 2 2 0 1 3 1 1 1