从查询创建视图 - oracle
Posted
技术标签:
【中文标题】从查询创建视图 - oracle【英文标题】:Create view from query - oracle 【发布时间】:2019-01-31 19:12:50 【问题描述】:我有在 oracle apex 中使用的查询。是否有可能从此查询中创建 jasper 报告的视图(Jasper 不支持 cte)。
注意:P99_DATE_FROM和P99_DATE_TO是参数!
with cte as (
SELECT
a.name,
w.order as order,
s.num as num,
w.id,
(Select count(*) from quant where id = w.id and trunc(date) between :P99_DATE_FROM and :P99_DATE_TO) as QUANT
FROM
web w
left outer JOIN tabel_1 a ON a.id = w.id
left outer JOIN tabel_2 s ON s.id = w.id
WHERE
(trunc(s.date) between :P99_DATE_FROM and :P99_DATE_TO or :P99_DATE_FROM is null or :P99_DATE_TO is null)
)
SELECT
name,
SUM(order) as TOTAL,
SUM(num) as NUM,
QUANT
from cte
group by name,QUANT
【问题讨论】:
为什么不直接使用内联视图呢? @AlexPoole 我不知道怎么用! 【参考方案1】:您可以parameterize a view,但使用 Apex 提供的参数会使它(甚至)更加复杂。
由于您只在主查询中引用一次 CTE,因此使用内联视图会更简单,将您的 CTE 查询变成子查询:
SELECT
name,
SUM(order) as TOTAL,
SUM(num) as NUM,
QUANT
from (
SELECT
a.name,
w.order as order,
s.num as num,
w.id,
(Select count(*) from quant where id = w.id and trunc(date) between :P99_DATE_FROM and :P99_DATE_TO) as QUANT
FROM
web w
left outer JOIN tabel_1 a ON a.id = w.id
left outer JOIN tabel_2 s ON s.id = w.id
WHERE
(trunc(s.date) between :P99_DATE_FROM and :P99_DATE_TO or :P99_DATE_FROM is null or :P99_DATE_TO is null)
)
group by name,QUANT
【讨论】:
【参考方案2】: WITH 子句的正常使用通过它的备用名称、子查询分解来揭示。当想要在我们的查询中多次使用子查询时,它们很方便,比如在两个whatever IN ( sub-query )
过滤器中。
这不适用于您的情况,因此您可以简单地将 CTE 转换为内联视图:
SELECT
name,
SUM(order) as TOTAL,
SUM(num) as NUM,
QUANT
from (
SELECT
a.name,
w.order as order,
s.num as num,
w.id,
(Select count(*)
from quant
where id = w.id and trunc(date) between :P99_DATE_FROM and :P99_DATE_TO) as QUANT
FROM
web w
left outer JOIN tabel_1 a ON a.id = w.id
left outer JOIN tabel_2 s ON s.id = w.id
WHERE
(trunc(s.date) between :P99_DATE_FROM and :P99_DATE_TO
or :P99_DATE_FROM is null or :P99_DATE_TO is null)
) cte
group by name,QUANT
【讨论】:
以上是关于从查询创建视图 - oracle的主要内容,如果未能解决你的问题,请参考以下文章