Please help me query this table
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
To:
11:00 | 5 | 4 |3 |8
11:05 | 7 | 2 |6 |4
You can safely assume that each row has only one value greater then 0. To make it easy to test, here's the table creation:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.