我有一张这样的表:
UPDATE
如何为每个传感器和fkey | sensor | depth | value
-----+--------+-------+-------
1 | 1 | 1 | 34
1 | 1 | 2 | 27
1 | 2 | 1 | 22
1 | 2 | 2 | 34
1 | 2 | 3 | 56
2 | 1 | 1 | 12
2 | 1 | 2 | 24
2 | 2 | 1 | 56
3 | 1 | 1 | 43
3 | 1 | 2 | 89
3 | 1 | 3 | 97
深度的GROUP_CONCAT
值编写选择查询以显示此信息?
ORDER BY
答案 0 :(得分:5)
尝试使用带有GROUP_CONCAT
表达式的CASE
来定位每个传感器数据。
SELECT
fkey,
GROUP_CONCAT(CASE WHEN sensor=1 THEN value END ORDER BY depth) AS sensor1_values,
GROUP_CONCAT(CASE WHEN sensor=2 THEN value END ORDER BY depth) AS sensor2_values
FROM yourTable
GROUP BY fkey;
答案 1 :(得分:0)
通过替换表名和列名
来尝试此查询fkey是A列传感器是B列深度是C列值是D列
select A, STUFF((SELECT ', ' + D FROM tableA b
WHERE b.A = a.A AND b.B = 1
FOR XML PATH('')), 1, 2, '') sensor1_values
, STUFF((SELECT ', ' + D FROM tableA b
WHERE b.A = a.A AND b.B = 2
FOR XML PATH('')), 1, 2, '') sensor2_values
from tableA a
group by A