我正在尝试将结果分成不同的栏目,我已经搜索了几天,但找不到我想做的正确术语。我可能使用错误的关键词。
我有一张这样的表:
Timestamp System Value
1 1 13
1 2 12
1 3 5
2 1 37
2 2 12
2 3 55
3 1 12
3 2 45
3 3 45
我希望得到这样的结果:
Timestamp, System1val, System2val, System3val
1 13 12 5
2 37 12 55
3 12 45 45
我很确定它可以做到。我一直在使用:
SELECT
FROM_UNIXTIME(timestamp) as time,
CASE System
WHEN '1' THEN System
END AS System1val,
CASE System
WHEN '2' THEN pyr.measure_conv
END AS System2val,
CASE System
WHEN '3' THEN pyr.measure_conv
END AS System3val
FROM
这实际上返回不同的字段并填入NULL值。最终目标是按时间戳分组并具有所有三个值
答案 0 :(得分:4)
这是一个“交叉表”挑战。你需要以这种方式构建东西......
SELECT
Timestamp,
SUM(CASE WHEN System=X THEN Value ELSE 0 END) AS X,
SUM(CASE WHEN System=Y THEN Value ELSE 0 END) AS Y
FROM System
GROUP BY Timestamp
答案 1 :(得分:2)
尝试
SELECT
timestamp time,
SUM(CASE WHEN system = 1 THEN value ELSE NULL END) System1val,
SUM(CASE WHEN system = 2 THEN value ELSE NULL END) System2val,
SUM(CASE WHEN system = 3 THEN value ELSE NULL END) System3val
FROM Table1
GROUP BY timestamp
输出:
| TIME | SYSTEM1VAL | SYSTEM2VAL | SYSTEM3VAL |
-----------------------------------------------
| 1 | 13 | 12 | 5 |
| 2 | 37 | 12 | 55 |
| 3 | 12 | 45 | 45 |
这是 SQLFiddle 演示