代码之家  ›  专栏  ›  技术社区  ›  Pointer

从查询创建视图-oracle

  •  1
  • Pointer  · 技术社区  · 5 年前

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

    注: P99_日期_至 是参数!

    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 回复  |  直到 5 年前
        1
  •  2
  •   Alex Poole    5 年前

    你可以 parameterize a view

    由于在主查询中只引用一次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
  •  2
  •   APC    5 年前

    WITH子句的正常使用是通过其备用名称子查询factoring来显示的。当我们想在查询中多次使用子查询时,比如两次使用子查询时,它们非常方便 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