I have a PostgreSQL Database table name Intime
Name | Intime1 | Intime2 | Intime3 | Intime4 | Intime5 | Intime6 |
---|---|---|---|---|---|---|
Zakir | 02/01/18:9.00 | 04/01/18:9.07 | 06/01/18:9.05 |
I want from this table:
Name | Intime |
---|---|
Zakir | 02/01/18:9.00 |
Zakir | 04/01/18:9.07 |
Zakir | 06/01/18:9.05 |
Now what is the query in the PostgreSQL?
Use a UNION:
select name, intime1 as intime
from intime
union all
select name, intime2
from intime
union all
select name, intime3
from intime
union all
select name, intime4
from intime
union all
select name, intime5
from intime
union all
select name, intime6
from intime
Another - Postgres specific - solution would be to use unnest
on an array of the columns:
select i.name, t.intime
from intime as i
cross join unnest(array[intime1,intime2,intime3,intime4,intime5,intime6]) as t(intime);
If you also want to know which column each row belongs to, you can use with ordinality
:
select i.name, t.intime, t.nr
from intime as i
cross join unnest(array[intime1,intime2,intime3,intime4,intime5,intime6]) with ordinality as t(intime,nr);
Online example: https://rextester.com/CDHVI81806