从查询创建视图-Oracle

时间:2019-01-31 19:12:51

标签: oracle view oracle-apex with-clause

我有在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

2 个答案:

答案 0 :(得分:2)

您可以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

答案 1 :(得分: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