SQL 使用子查询的条件创建列
SQL Create a column with conditions from a sub query
我有一个 table,带有 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
我有一个 table,带有 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