[英]SQL Create a column with conditions from a sub query
我有一个包含 ID 和高度、长度和宽度的表。 我需要找到每一行的 mas 度量值,然后如果最大度量值介于 22 和 30 之间则创建一列 5 美元的附加费,如果大于 30 则创建 8 美元。 第一部分工作正常
select id, max(measure) as max_measure
from (
select id, height as measure from table1
union
select id, length as measure from table1
union
select id, width as measure from table1
) m
group by id
但是我不能做第二部分,它应该是一个子查询,使用我从第一部分得到的结果,看起来大致像这样
select surcharge where
m.max_measure >= 22 and m.max_measure <30
m.max_measure>= 30
select id,
max(measure) as max_measure,
case when max(measure) >= 30 then 8
when max(measure) >= 22 then 5
else 0 end as surcharge
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.