【问题标题】:Select most current data in grouped set in Oracle选择Oracle分组集中的最新数据
【发布时间】:2013-12-26 19:25:29
【问题描述】:

我正在编写一个程序来查询Oracle中的一些数据并对其进行分组:

Account  Amt Due   Last payment  Last Payment Date (mm/dd/yyyy format)
1234      10.00       5.00          12/12/2013
1234      35.00       8.00          12/12/2013
3293      15.00      10.00          11/18/2013
4455       8.00       3.00          5/23/2013
4455      14.00       5.00          10/18/2013

我想对数据进行分组,因此每个帐户有一条记录,汇总了应付的 Amt 以及最后一次付款。除非最后一次付款日期不同——如果日期不同,那么我只想要最后一次付款。所以我想要这样的结果:

Account  Amt Due   Last payment  Last Payment Date
1234      45.00      13.00          12/12/2013
3293      15.00      10.00          11/18/2013
4455      22.00       5.00          10/18/2013

我正在做类似的事情

select Account, sum (AmtDue), sum (LastPmt), Max (LastPmtDt)
from all my tables
group by Account

但是,这不适用于上面的最后一条记录,因为最后一次付款只是 10/18 的 5.00 美元,而不是 10/18 的总和。

如果我按 Account 和 LastPmtDt 分组,那么我会得到最后一条记录,但我只希望每个账户有一条。

我有其他要查询的数据,并且我在另一个字段上使用了 CASE、INSTR 和 LISTAGG(如果将它们组合给我这个子字符串和那个,那么输出“Both”;否则如果它只给我这个子字符串,然后输出子字符串;否则,如果它只给我另一个子字符串,则输出那个)。看来我可能需要类似的东西,但不是通过寻找特定日期。如果日期相同,那么 sum (LastPmt) 和 max (LastPmtDt) 工作正常,如果它们不一样,那么我想忽略除最近的 LastPmt 和 LastPmtDt 记录之外的所有记录。

哦,我的 LastPmt 和 LastPmtDt 字段已经是 select 中的 case 语句。它们不是我已经可以访问的字段。我正在阅读有关 RANK 和 KEEP 的其他帖子,但要涉及这两个字段,我还需要对每个字段进行所有计算。查询所有内容,然后围绕它包装另一个查询来进行分组、求和和选择我想要的字段会更有效吗?

相关:HAVING - GROUP BY to get the latest record

有人可以就如何解决这个问题提供一些指导吗?

【问题讨论】:

    标签: sql oracle oracle11g group-by


    【解决方案1】:

    试试这个:

    select Account, 
           sum ( Amt_Due), 
           sum (CASE WHEN Last_Payment_Date = last_dat THEN Last_payment ELSE 0 END), 
           Max (Last_Payment_Date)
    from (
      SELECT t.*,
           max( Last_Payment_Date ) OVER( partition by Account ) last_dat
      FROM table1 t
    )
    group by Account
    

    演示 --> http://www.sqlfiddle.com/#!4/fc650/8

    【讨论】:

      【解决方案2】:

      排名是正确的想法。

      试试这个

      select a.Account, a.AmtDue, a.LastPmt, a.LastPmtDt from (
        select Account, sum (AmtDue) AmtDue, sum (LastPmt) LastPmt, LastPmtDt, 
          RANK() OVER (PARTITION BY Account ORDER BY LastPmtDt desc) as rnk
        from all my tables
        group by Account, LastPmtDt
        ) a
      where a.rnk = 1
      

      我没有测试过这个,但它应该给你正确的想法。

      【讨论】:

        【解决方案3】:

        试试这个:

        select Account, sum(AmtDue), sum(LastPmt), LastPmtDt
          from (select Account,
                       AmtDue,
                       LastPmt,
                       LastPmtDt,
                       max(LastPmtDt) over(partition by Account) MaxLastPmtDt
                  from your_table) t
         where t.LastPmtDt = t.MaxLastPmtDt
         group by Account, LastPmtDt
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-17
          • 2017-05-22
          • 1970-01-01
          • 1970-01-01
          • 2018-07-15
          相关资源
          最近更新 更多