首页 > 技术文章 > SQL:行列转换-操作查询结果case when then else end用法举例

wobushitiegan 2020-02-17 11:30 原文

SQL:行列转换

现在的结果:

想要的结果:

把现在的结果,每取4列放在一行。

select
    max(case QUESTIONNO when 'TJBGSAB1' then  QUESTIONANSWER end),
    max(case QUESTIONNO when 'TJBGSAB2' then  QUESTIONANSWER end),
    max(case QUESTIONNO when 'TJBGSAB3' then  QUESTIONANSWER end),
    max(case QUESTIONNO when 'TJBGSAB4' then  QUESTIONANSWER end)
from LCREPORTQUESTIONNAIRE
where CONTNO = '00000000001114'
  and idno = '0000055911'
  and QUEMODULECODE ='TJBGSA'
  and QUESUBMODULECODE like 'B%'
group by QUESUBMODULECODE;

推荐阅读