[英]Using CASE WHEN statements (MySQL)
Struggling with a case when statement (MySQL) and its probably something simple I am missing. 挣扎于一个当when语句(MySQL)及其可能缺少的简单事情的情况下。
I have a fairly complex query which outputs some sensor values and low and high limits (that will eventually trip alarms), as part of that query I use - 我有一个相当复杂的查询,它输出一些传感器值以及上下限(最终将触发警报),作为我使用的查询的一部分,
case when sequence=0 then value END as lowlimit,
case when sequence=1 then value END as highlimit
The output from the select is - 选择的输出是-
status sensor lowlimit highlimit
163 49 NULL 24
163 49 15 NULL
128 25 NULL 8
128 25 4 NULL
but I need the results to be 'concatenated' (a single row for each unique status), thus - 但我需要将结果“串联”(每个唯一状态的一行),因此-
status sensor lowlimit highlimit
163 49 15 24
128 25 4 8
so probably need something adding to the case statements, but if I use 所以可能需要在case语句中添加一些内容,但是如果我使用
max(case when sequence=0 then value END) as lowlimit,
max(case when sequence=1 then value END) as highlimit
then only 1 row is output - 那么仅输出1行-
status sensor lowlimit highlimit
163 49 15 24
any assistance appreciated. 任何帮助表示赞赏。
Regards Active 关于活动
If you need the result on the same row You should use a seleft join (not case) 如果在同一行上需要结果,则应使用seleft连接(不区分大小写)
select a.status, a.sensor, a.values as lowlimit, b.value as highlimit
from my_table a
inner join (
select status, sensor, value from my_table
where sequence = 1
) b on a.sensor = b.sensor and a.status = b.status
where a.sequence = 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.