gpt4 book ai didi

mysql单列值到多列组由另一个列值

转载 作者:行者123 更新时间:2023-11-29 07:29:42 24 4
gpt4 key购买 nike

我正在尝试从这样的表创建查询

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

我想选择日期之间的值,按日期对它们进行分组,并在分隔的列上放置 id

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

所以,我希望不同 id 的值按列分组(基于 id )没有值的地方返回null谢谢你们

最佳答案

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

关于mysql单列值到多列组由另一个列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52038963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com