2013-05-21 99 views
0

我是新来的oracle数据库动态查询,我想动态数据

select o.id as ovaid , 
(case when(select count(m.cid) from ovamapper m where m.id = o.id and m.solutionid = 1)>0 then 1 else 0 end) as sol1, 
(case when(select count(m.cid) from ovamapper m where m.id = o.id and m.solutionid = 2)>0 then 1 else 0 end) as sol1, 
(case when(select count(m.cid) from ovamapper m where m.id = o.id and m.solutionid = 3)>0 then 1 else 0 end) as sol1 from ovatemplate o order by o.id 

而是执行了solutionid静态值的下面的查询,我想从其他表中选择它。

任何帮助是非常感谢

+0

请提供更清晰的表格架构 –

回答

0

你可以使用

加入

到包含solutionid表。前

Select * from ovatemplate JOIN solutiontable ON (solutiontable.ovaid=ovatempate.ovaid) 

后,改变静态值solutionid

0

尝试此查询

select o.id as ovaid , 
     count(case when solutionid = 1 then m.cid else null end) as sol1 , 
     count(case when solutionid = 2 then m.cid else null end) as sol2 , 
     count(case when solutionid = 3 then m.cid else null end) as sol3 
from ovamapper m , ovatemplate o 
where m.id = o.id 
group by o.id 
order by o.id 

如果你不需要的聚合为列,你或许应该这样做,而不是

select o.id as ovaid , solutionid , count(*) as sol 
from ovamapper m , ovatemplate o 
where m.id = o.id 
and  m.solutionid in (1,2,3) 
group by o.id , solutionid 
order by o.id 
+0

感谢您的回应...但在上述查询中,solu tionids是静态的...我想从不同的表中得到解决方案,比如“从table2中选择solutionid” – SUSER