我想在代码中编写一个SQL语句,其作用类似于某种类型的case语句,以便识别某个“指标”代表两种不同的“含义”。当下表中的指标=“底层”时,我想要添加一个额外的行来取代其他样式
例如(下面的表名称称为“样式”):
我想对这个表运行一个select查询,它会返回每当指示符='底层'时创建一个额外MDY行的结果,并且基本上将指示符和类型拆分为两个不同的行。因此,只要存在一个行,其中indicator = Underlying and agency = MDY,我想返回两个MDY行的输出(一行type = long,indicator = NULL,另一行type = NULL,indicator = underlying)
所以输出看起来像这样:
答案 0 :(得分:0)
以下是使用union all
的一个选项:
select name, style, agency, type, indicator
from styles
where indicator != 'underlying' and type != 'long'
union all
select name, style, agency, type, null
from styles
where indicator = 'underlying' and type = 'long'
select name, style, agency, null, indicator
from styles
where indicator = 'underlying' and type = 'long'