簡體   English   中英

動態查詢動態數據

[英]Dynamic query to dynamic data

我是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的靜態值。

對此的任何幫助都非常感謝

你可以用

加入

到包含solutionid的表。

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

之后,將靜態值更改為solutionid

試試這個查詢

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM