【问题标题】:Get current data and last month data for same column获取同一列的当前数据和上个月数据
【发布时间】:2019-11-06 13:58:01
【问题描述】:

我正在比较 Oracle SQL 中 11 月 (11) 和 10 月 (10) 的 AMOUNTPROJECT_ID 的值。

我尝试过子查询,但AMOUNT_LAST_MONTH 显示相同的结果。

select 
    PROJECT_ID, 
    sum(AMOUNT), 
    ( 
        select sum(amount) 
        from APPS.pa_draft_revenue_items
        where 
            to_char(LAST_UPDATE_DATE,'MM')='10' 
            AND to_char(LAST_UPDATE_DATE,'YYYY')='2019'
    ) AMOUNT_LAST_MONTH
from 
    APPS.pa_draft_revenue_items 
where 
    PROJECT_ID IN (
        select PROJECT_ID 
        from APPS.pa_draft_revenue_items
        where 
            to_char(LAST_UPDATE_DATE,'MM')='11' 
            AND to_char(LAST_UPDATE_DATE,'YYYY')='2019' 
    ) 
GROUP by PROJECT_ID, amount ;

我希望AMOUNT_LAST_MONTH 具有不同的值。

【问题讨论】:

  • 尝试使用别名如下:主表 pa_draft_revenue_items 主表 pa_draft_revenue_items 在子查询中上个月。因此,在子查询中添加条件 main.pa_draft_revenue_items.project_id = lastmonth.pa_draft_revenue_items .project_id

标签: sql oracle date subquery


【解决方案1】:

如果我没听错的话,你的查询可以通过条件聚合大大简化:

select 
    project_id, 
    sum(case when extract(month from last_update_date) = 10 then amount end) amount_last_month, 
    sum(case when extract(month from last_update_date) = 11 then amount end) amount_this_month
from apps.pa_draft_revenue_items
where 
    last_update_date >= to_date('2019-10-01', 'yyyy-mm-dd')
    and last_update_date < to_date('2019-12-01', 'yyyy-mm-dd')
group by project_id

【讨论】:

    【解决方案2】:

    我会坚持你使用sysdate,因为它会变得通用,如下所示:

    select 
        project_id, 
        sum(case when trunc(last_update_date,'month') = trunc(add_months(sysdate, -1),'month') then amount end) amount_last_month, 
        sum(case when trunc(last_update_date,'month') = trunc(sysdate,'month') then amount end) amount_this_month
    from apps.pa_draft_revenue_items
    where 
        last_update_date >= trunc(add_months(sysdate, -1),'month')
        and last_update_date < trunc(add_months(sysdate, 1),'month')
    group by project_id
    

    干杯!!

    【讨论】:

      【解决方案3】:

      子查询的结果集必须与主查询的一行链接,链接用project_id表示

      您更改的查询:

      select 
          PROJECT_ID, 
          sum(AMOUNT), 
          ( 
              select sum(amount) 
              from APPS.pa_draft_revenue_items lastmonth
              where to_char(LAST_UPDATE_DATE,'MM')='10' 
              AND to_char(LAST_UPDATE_DATE,'YYYY')='2019'
              AND lastmonth.project_id = main.project_id
          ) AMOUNT_LAST_MONTH
      from APPS.pa_draft_revenue_items main
      where PROJECT_ID IN (
          select PROJECT_ID 
          from APPS.pa_draft_revenue_items
          where 
              to_char(LAST_UPDATE_DATE,'MM')='11' 
              AND to_char(LAST_UPDATE_DATE,'YYYY')='2019' 
      ) 
      GROUP by PROJECT_ID, amount ;
      

      【讨论】:

      • 太棒了!谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      相关资源
      最近更新 更多