I have a table like this:
+----+------------+-----------------------+
| id | date | date_time |
+----+------------+-----------------------+
| 1 | 2016-07-01 | 2016-07-01 06:00:00 |
| 2 | 2016-07-01 | 2016-07-01 06:02:00 |
| 3 | 2016-07-01 | 2016-07-01 06:01:00 |
| 1 | 2016-07-01 | 2016-07-01 16:25:00 |
| 2 | 2016-07-01 | 2016-07-01 18:04:00 |
| 3 | 2016-07-01 | 2016-07-01 12:06:00 |
| 3 | 2016-07-01 | 2016-07-01 17:57:00 |
+----+------------+-----------------------+
I wanna do something like this using sql:
+----+------------+---------------------+------------------------+----------------------+
| id | date | date_time1 | date_time2 | date_time3 |
+----+------------+---------------------+------------------------+----------------------+
| 1 | 2016-07-01 | 2016-07-01 06:00:00 | 2016-07-01 16:25:00 | |
| 2 | 2016-07-01 | 2016-07-01 06:02:00 | 2016-07-01 18:04:00 | |
| 3 | 2016-07-01 | 2016-07-01 06:01:00 | 2016-07-01 17:57:00 | 2016-07-01 17:57:00 |
+----+------------+---------------------+------------------------+----------------------+
The table can have two, three or four times for each day. I was trying to do something with postgres, but I didn't have good results.
Can someone help me?
ctrl-kor add 4 spaces to format code as textPIVOT, and probablyDinamic PIVOTAlso you should include your RDBMS name so we can provide more specific help. Please read How-to-Ask And here is a great place to START to learn how improve your question quality and get better answers.select id, date, array_agg(date_time) from ... group by id, date;(BTW it is the ready to use answer if replacearray_agg(date_time)by the series of(array_agg(date_time))[1], (array_agg(date_time))[2], ...). So the matter is in the data representation.