在CTE构造中基于字段值乘以不同的字段

时间:2013-09-29 01:13:01

标签: sql firebird

在CTE构造中,我想知道是否有办法根据主查询中的字段值将CTE定义中的字段与主查询中的字段之间的不同字段相乘?

例如,在CTE构造中,有Width1Width2个字段。在主要查询中,有Height1Height2DIM_Type

是否可以从Result1获得两个计算字段Result2Width1 * Height1 DIM_Type = 1以及Width2 * Height2如果DIM_Type = 2?< / p>

2 个答案:

答案 0 :(得分:3)

select case 
    when DIM_Type = 1 then Width1 * Height1
    else  Width2 * Height2
end CalculatedField
from cte

答案 1 :(得分:1)

我做了一个小提琴,或多或少和上面一样:http://sqlfiddle.com/#!6/d24e7/10 您需要使用分别包含height1和heigh2的列来更改111和222

with propCte as (

  select 
    width1,
    width2,
    dim_type
  from 
    props

)

select
  case dim_type
    when 2 then propCte.width1 * 111
    else propCte.width2 * 222
  end as computed
from 
  propCte
相关问题