簡體   English   中英

SQL 使用子查詢條件創建列

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM