简体   繁体   English

SQL 使用子查询条件创建列

[英]SQL Create a column with conditions from a sub query

I have a table with ID and HEIGHT, LENGTH and WIDTH.我有一个包含 ID 和高度、长度和宽度的表。 I need to find the mas measure of every row and then create a column of surcharge of $5 if the biggest measure is between 22 and 30 and 8 if it is >30.我需要找到每一行的 mas 度量值,然后如果最大度量值介于 22 和 30 之间则创建一列 5 美元的附加费,如果大于 30 则创建 8 美元。 The first parte is working fine第一部分工作正常

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

But i cant make the second part, it should be a sub query using the results I got from the first part and looking something roughly like this但是我不能做第二部分,它应该是一个子查询,使用我从第一部分得到的结果,看起来大致像这样

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM