简体   繁体   中英

create a new column within a select statement

Heres the code im using.

 select address, area,
 case area
 when area < 1500 then 'small'
 when area > 2500 then 'large'
 else 'medium'
 end as size
 from listings
 where zip = 95677;

With this i want to create a new column that will tell you whether a house is big or small depending on the area size.

This is the error i get and im not really sure how to cast it to make it work. If that is the solution to it like the hint says.

LINE 3: when area < 1500 then 'small' ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

The correct syntax is:

 select address, area,
        (case when area < 1500 then 'small'
              when area > 2500 then 'large'
              else 'medium'
         end) as size
 from listings
 where zip = 95677;

case has two forms. The above is the case when <condition> form, where the when clauses are fully formed conditions. The other form is more like a switch statement in C: case area when 10 then 'ten' when 20 then 'twenty' . . . case area when 10 then 'ten' when 20 then 'twenty' . . . . This form can only test for equality of a single expression.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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