brebenel cristian
brebenel cristian

Reputation: 21

mysql single column values to multiple columns group by another column value

i am trying to create a query from a table looking like this

timestamp             |         iddebi       | value 
========================================================
2018-02-17  14:29:00             1               901
2018-02-19  14:29:00             1               501
2018-02-20  14:29:00             1               301
2018-02-21  14:29:00             1               501
2018-02-18  14:29:00             2               301
2018-02-19  14:29:00             2               401
2018-02-20  14:29:00             2               601
2018-02-21  14:29:00             2               701
2018-02-18  14:29:00             3               901
2018-02-19  14:29:00             3               501
2018-02-20  14:29:00             3               301
2018-02-21  14:29:00             3               501

i want to select values between dates , group them by date, and id on separated columns

timestamp            |  iddebi_1  |  iddebi_2  | iddebi_3
===========================================================
2018-02-17  14:29:00     901         null        null
2018-02-18  14:29:00     null         301        901
2018-02-19  14:29:00     501         401        501
2018-02-20  14:29:00     301         601        301
2018-02-21  14:29:00     501         701        501

so, i want the values of different ids grouped by columns (based on id ) and where there is no value to return null thank you guys

Upvotes: 1

Views: 152

Answers (1)

juergen d
juergen d

Reputation: 204746

select timestamp,
       max(case when iddebi = 1 then value else null end) as iddebi_1,
       max(case when iddebi = 2 then value else null end) as iddebi_2,
       max(case when iddebi = 3 then value else null end) as iddebi_3
from your_table
group by timestamp 

Upvotes: 1

Related Questions