我们的代码结构是这样的:
select
sampleA.columnA,
(select sampleB.columnA
from sampleB where sampleB.ColumnB = 'test1'),
(select sampleB.columnA
from sampleB where sampleB.ColumnC = 'test2')
from sampleA
join sampleB on sampleA.id = sampleB.id
where sampleA.columnB = 'test3'
如果我们只运行,那么会发生什么
select sampleB.columnA from sampleB where sampleB.ColumnB = 'test1'
然后返回27个结果。
同样,它返回其他一些说35行
select sampleB.columnA from sampleB where sampleB.ColumnC = 'test2'
但是当我们运行整个查询时,我们会收到标量错误....
我们正在使用DB2并且最终希望在Jasper上运行它...并且Jasper不支持'使用'任何其他方式?
答案 0 :(得分:0)
子查询必须只返回一行,请尝试:
select sampleA.columnA,
(select sampleB.columnA from sampleB where sampleB.ColumnB = 'test1' FETCH FIRST ROWS ONLY) as columnAtest1 ,
(select sampleB.columnA from sampleB where sampleB.ColumnC = 'test2' FETCH FIRST ROWS ONLY) as columnAtest2
from sampleA
join sampleB on sampleA.id = sampleB.id
where sampleA.columnB = 'test3'
或者
select sampleA.columnA,
Samplec.columnA as columnAtest1, Sampled.columnA as columnAtest2
from sampleA
join sampleB on sampleA.id = sampleB.id
cross join (select sampleB.columnA from sampleB where sampleB.ColumnB = 'test1' FETCH FIRST ROWS ONLY) Samplec
cross join (select sampleB.columnA from sampleB where sampleB.ColumnB = 'test2' FETCH FIRST ROWS ONLY) Sampled
where sampleA.columnB = 'test3'