我正在尝试构建一个看起来像这样的视图:
CREATE OR REPLACE VIEW TestView AS
SELECT
p.name,
?? (select count(*) from Test t where t.currentstate in ('Running', 'Ended') and t.ref = p.key) as HasValues
from ParentTable p
如果当前状态的计数为>,我希望列HasValues为1或0。 0
有人能告诉我怎么做吗? 感谢
答案 0 :(得分:0)
CREATE OR REPLACE VIEW TestView AS
SELECT
p.name,
case
when nvl(t.mycount,0) > 0 then '1'
else '0'
end HasValues
from ParentTable p
left outer join (select ref, count(*) mycount from Test group by ref) t on t.ref = p.key
答案 1 :(得分:0)
如果您在测试表中对于父表中的每一行都有很多行,并且测试表上的连接键已编制索引,那么将查询构造为以下内容可能是最有效的:
CREATE OR REPLACE VIEW TestView AS
SELECT
p.name,
(select count(*)
from Test t
where t.currentstate in ('Running', 'Ended') and
t.ref = p.key and
rownum = 1) as HasValues
from ParentTable p;
此查询的HasValues始终为0或1。
如果test和父表之间的行比率小于约10:1 和我想为父表中的所有行运行它,那么我只是加入这两个表像StevieG的回答一样
答案 2 :(得分:0)
-- This has the added advantage that it doesn't count all records,
-- it only needs to see one.
CREATE OR REPLACE VIEW testview
AS
SELECT p.name
, CASE
WHEN EXISTS
(SELECT NULL
FROM test t
WHERE t.currentstate IN ('Running', 'Ended')
AND t.REF = p.key)
THEN
1
ELSE
0
END
hasvalues
FROM parenttable p