Sql 在db2中推广条件查询

Sql 在db2中推广条件查询,sql,db2,Sql,Db2,我正在IBM DB2上工作。我有一个问题: select temp.dv, max(case when att.id=100 then temp.en else '0' end) as cl, max(case when att.id=103 then temp.en else null end) as mo, max(case when att.id=104 then temp.en else null end) as do, from temp left out

我正在IBM DB2上工作。我有一个问题:

select temp.dv,
    max(case when att.id=100 then  temp.en else '0' end) as cl,
    max(case when att.id=103 then  temp.en else null end) as mo,
    max(case when att.id=104 then  temp.en else null end) as do,
from temp left outer join att on att.id=temp.id where doc=100

如何将其推广到
att.id
的所有值?

如果需要数据透视,则可以将列名复制到Excel并生成正确的语法。您还可以将SQL语句放入字符串中,然后运行该字符串(在其他数据库中称为动态SQL)

如果不需要旋转它们,则可以使用
groupby
将结果放在单独的行上:

select temp.dv,
       att.id,
       max(temp.en)
    max(case when att.id=104 then  temp.en else null end) as do,
from temp left outer join
     att
     on att.id=temp.id
where doc=100
group by temp.dv, att.id

你能告诉我如何在这个问题上使用动态sql吗?我对sql没有太多的经验。@khateeb。我没有在DB2中编写语句的经验。文档在这里()。它需要更多的细节。
att.id
值的每个示例显示不同的结果。因此,不可能“概括……所有价值观”。这个例子并没有演示泛化,而是专门化。可能需要显示预期结果的多个输入数据样本。