I have a table called code_status:
Code Message
1 "Start"
2 "In process"
3 "Finish"
And another table called history
ID Code Name time_date
1 2 Jon 1/2/15
31 1 Abby 2/1/15
12 3 Sara 3/3/15
31 2 Abby 2/3/15
31 3 Abby 2/5/15
8 2 Max 1/22/15
I want to create an history_view with the following schema:
history_view (id, name, start_date, process_date, finish_date)
If the date is not given, it would just be NULL
So it would look like
ID Name start_date process_date finish_date
31 Abby 2/1/15 2/3/15 2/5/15
1 Jon NULL 1/2/15 NULL
... etc
So i started off by doing :
CREATE VIEW history_view
AS SELECT h.id, h.name,
(CASE WHEN h.code = 1 THEN time_date) AS start_date,
(CASE when h.code = 2 THEN time_date) AS process_date,
(CASE when h.code = 3 THEN time_date) AS finish_date
FROM history h;
I would get a result like the following below though:
ID Name start_date process_date finish_date
31 Abby 2/1/15 NULL NULL
31 Abby NULL 2/3/15 NULL
31 Abby NULL NULL 2/5/15
... ETC
Is there any way to consolidate the rows together ?