从查询创建视图 - oracle

Create view from query - oracle

我有在 oracle apex 中使用的查询。是否可以从此查询创建视图,其中用于 jasper 报告(Jasper 不支持 cte)。

注意:P99_DATE_FROMP99_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

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

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