SQL嵌套的select语句;

时间:2014-04-14 10:56:05

标签: sql db2

我需要根据此数据表架构创建一个新表。 我不确定我是否可以使用嵌套的sql select语句来做到这一点。

screenshot

2 个答案:

答案 0 :(得分:2)

select ColA, ColB, ColC, ColD, (select ColE from table where colA=120) ColE
from table
where colA = 122

只需使用嵌套选择colE并为列提供别名。

答案 1 :(得分:0)

如果ColA中有多个值,您还没有说明找到ColE的条件。我假设(基于你的表)你想要最小的价值?

select min(ColE) 
from table
where ColA=120
group by ColA;

现在使用上面的内容你可以创建一个嵌套的选择

select ColC, ColB, ColC,ColD , minColE
from table, (
    select min(ColE) as minColE
    from table
    where ColA=120
    group by ColA
) as TblA
where ColA=122;
相关问题