1

我有查询在 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
4

2 回答 2

2

您可以参数化视图,但使用 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
于 2019-01-31T19:19:17.553 回答
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
于 2019-01-31T19:19:24.507 回答