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 的静态值。

对此的任何帮助都非常感谢

4

2 回答 2

0

你可以使用

加入

到包含 solutionid 的表。前任

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

之后,将静态值更改为 solutionid

于 2013-05-21T02:29:39.707 回答
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
于 2013-05-21T05:57:56.163 回答