【问题标题】:Month To Date Report PLSQL月至今报告 PLSQL
【发布时间】:2020-06-30 12:39:02
【问题描述】:

我正在使用 PLSQL,我正在尝试从下表 (TAX) 创建月至今和年至今报告:

Date            District         City          Amount
2020-01-02       West            Auckland      1000
2020-01-03       East            Auckland      1200
2020-01-04       North           Auckland      1300
2020-02-02       West            Auckland       500
2020-02-03       East            Auckland       300
2020-02-04       North           Auckland       200
2020-01-02       West            Wellington    2000
2020-01-03       East            Wellington     500
2020-01-04       North           Wellington    2500
2020-02-02       West            Wellington     300
2020-02-03       East            Wellington    1000
2020-02-04       North           Wellington     500

我想要得到的是按城市分组的最近一天、本月至今和年初至今(总结),如下所示:

City             2020-02-04   Month_To_Date    Year_To_Date
Auckland           200         1000             3000
Wellington         500         2800             7800

有没有办法使用 Oracle PLSQL 做到这一点?

【问题讨论】:

    标签: plsql group-by pivot-table


    【解决方案1】:

    你想要的基本是一个相对简单的查询

    select city      
          , <current_date>  
          , sum(amount) amt 
       from tax
       cross join for_date
       where tax_date = <current_date>
       group by city, <current_date>  
    

    但是,这需要对总计的每个子集(上面的 amt)稍作修改,因为
    tax_date = ,需要针对不同的日期期间进行调整。此外,由于这些不同的条件,您无法在单个选择中执行此操作。解决方案是为每个日期范围构建一个 CTE,然后连接在一起。
    (注意:我将变量/列名称更改为 tax_date,因为日期是具有集合定义和保留字的数据类型。将其用作变量/列名称是非常糟糕的做法。所以提示:永远不要将数据类型用作对象名称。)

    with tax   as
      ( select date '2020-01-02' tax_date
             , 'West' district,'Auckland' city
             , 1000 amount 
          from dual union all                                   
        select date '2020-01-03','East','Auckland',1200    from dual union all 
        select date '2020-01-04','North','Auckland',1300   from dual union all
        select date '2020-02-02','West','Auckland',500     from dual union all
        select date '2020-02-03','East','Auckland',300     from dual union all
        select date '2020-02-04','North','Auckland',200    from dual union all
        select date '2020-01-02','West','Wellington',2000  from dual union all
        select date '2020-01-03','East','Wellington',500   from dual union all
        select date '2020-01-04','North','Wellington',2500 from dual union all
        select date '2020-02-02','West','Wellington',300   from dual union all
        select date '2020-02-03','East','Wellington',1000  from dual union all
        select date '2020-02-04','North','Wellington',500  from dual 
       )
       , for_date as (select date '&current_date' dt from dual)
       , dly as 
              ( select city      
                     , dt   
                     , sum(amount) amt 
                  from tax
                  cross join for_date
                 where tax_date = dt 
                 group by city, dt              
              )
       , mtd as 
              ( select city      
                     , dt   
                     , sum(amount) amt
                  from tax                 
                 cross join for_date             
                 where trunc(tax_date,'mm') = trunc(dt,'mm') 
                   and tax_date <= dt
                 group by city, dt
              )
       , ytd as 
              ( select city      
                     , dt   
                     , sum(amount) amt 
                  from tax
                  cross join for_date             
                 where trunc(tax_date,'yyyy') = (select trunc(dt,'yyyy') from for_date)
                   and tax_date <= dt 
                 group by city, dt
              )
    select  
           dly.city       "City"
         , dly.dt         "Tax Date"
         , dly.amt        "Daily Amount"
         , mtd.amt        "Month to Date"
         , ytd.amt        "Year To Date"
      from dly
      join mtd on (dly.city = mtd.city)
      join ytd on (dly.city = ytd.city)
     order by dly.city;    
    

    区别:结果将当前日期添加为列,而不是尝试将其作为列名。对于 SQL,这是主要的跳跃(如果它甚至是可行的)。但是对于您的表示层来说,这种转换应该非常简单。

    顺便说一句:检查你的数学它是关闭的。示例:惠灵顿的本月至今为 1800 (500+1000+300) 而不是 2800,并在例外结果中显示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-19
      相关资源
      最近更新 更多