SQL: Alias Column Name for Use in CASE Statement

前端 未结 11 1806
悲哀的现实
悲哀的现实 2020-12-04 21:38

Is it possible to alias a column name and then use that in a CASE statement? For example,

SELECT col1 as a, CASE WHEN a = \'test\' THEN \'yes\' END as value          


        
11条回答
  •  余生分开走
    2020-12-04 21:44

    I use CTEs to help compose complicated SQL queries but not all RDBMS' support them. You can think of them as query scope views. Here is an example in t-sql on SQL server.

    With localView1 as (
     select c1,
            c2,
            c3,
            c4,
            ((c2-c4)*(3))+c1 as "complex"
       from realTable1) 
       , localView2 as (
     select case complex WHEN 0 THEN 'Empty' ELSE 'Not Empty' end as formula1,
            complex * complex as formula2    
       from localView1)
    select *
    from localView2
    

提交回复
热议问题