2013-05-21 71 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