gpt4 book ai didi

mysql - 基于零合并 SQL 表行

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

请帮我查询一下这个表

11:00 | 5 | 0 |0 |0
11:00 | 0 | 4 |0 |0
11:00 | 0 | 0 |3 |0
11:00 | 0 | 0 |0 |8
11:05 | 7 | 0 |0 |0
11:05 | 0 | 2 |0 |0
11:05 | 0 | 0 |6 |0
11:05 | 0 | 0 |0 |4

致:

11:00 | 5 | 4 |3 |8
11:05 | 7 | 2 |6 |4

您可以安全地假设每一行只有一个大于 0 的值。为了方便测试,下面是表创建:

CREATE TABLE MyTable (Time NVARCHAR(10), Value1 INT, Value2 INT, Value3 INT, Value4 INT)
INSERT INTO MyTable VALUES ('11:00',5,0,0,0)
INSERT INTO MyTable VALUES ('11:00',0,4,0,0)
INSERT INTO MyTable VALUES ('11:00',0,0,3,0)
INSERT INTO MyTable VALUES ('11:00',0,0,0,8)
INSERT INTO MyTable VALUES ('11:05',7,0,0,0)
INSERT INTO MyTable VALUES ('11:05',0,2,0,0)
INSERT INTO MyTable VALUES ('11:05',0,0,6,0)
INSERT INTO MyTable VALUES ('11:05',0,0,0,4)

最佳答案

select Time, 
max(Value1) as value1,
max(Value2) as value2,
max(Value3) as value3,
max(Value4) as value4
from MyTable
group by Time

关于mysql - 基于零合并 SQL 表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25428697/

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