简体   繁体   English

动态查询动态数据

[英]Dynamic query to dynamic data

I am new to the oracle database, I am trying to execute the following query 我是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

Instead of static values for solutionid , I would like to select it from other table. 我想从其他表中选择它而不是solutionid的静态值。

Any help on this is really appreciated 对此的任何帮助都非常感谢

you could use 你可以用

join 加入

to table that contain the solutionid. 到包含solutionid的表。 ex

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

after that, change the static values to solutionid 之后,将静态值更改为solutionid

Try this query 试试这个查询

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

If you dont need the aggregations as columns you should probably do that instead 如果您不需要将聚合作为列,则应该这样做

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM