使用Oracle SQL中的rank()和子查询从更新日期检索上次余额

问题描述 投票:0回答:3

我在从桌子上检索balance信息时遇到了麻烦。数据集看起来像这样:

| Name          | Last Name     | Balance | Update Date |
+---------------+---------------+---------+-------------+
| John          | Doe           | $1600   | 2017-01-01  |
| John          | Doe           |   $12   | 2017-01-02  |
| John          | Doe           |    $1   | 2017-01-03  |
| John          | Doe           |   $16   | 2017-01-04  |
| John          | Doe           |   $16   | 2017-01-05  |
| John          | Doe           |   $16   | 2017-01-06  |

任务是用Balance获取最新的Update Date,但如果相同的Balance在几天内相同,那么在这种情况下我们需要用这个Update Date得到第一个Balance,所以在这种情况下,我们需要以下结果:

| Name          | Last Name     | Balance | Update Date |
+---------------+---------------+---------+-------------+
| John          | Doe           |   $16   | 2017-01-04  |

我试图使用我的查询:

select 
    a.name,
    a.last_name,
    a.balance,
    a.update_date
from
    (select
        name,
        last_name,
        balance,
        update_date,
        rank () over (partition by name, last_name order by update_date desc) top
    from
        customer_balance) a
where
    a.top = 1

但它显然会回归:

| Name          | Last Name     | Balance | Update Date |
+---------------+---------------+---------+-------------+
| John          | Doe           |   $16   | 2017-01-06  |

我不确定如何修改它以获得所需的结果。请注意,我的访问权限有限,因此不允许使用临时表,函数或类似内容。只是简单的选择,没什么特别的。

我很感激你的帮助。

sql oracle window-functions rank
3个回答
2
投票

您可以通过使用Tabibitosan查找包含最新update_date行的相同余额的行组(整个数据集顶部的行与最新余额之间的差异为0)然后按选择最早的update_date,如下所示:

WITH customer_balance AS (SELECT 'John' first_name, 'Doe' last_name, 1600 balance, to_date('01/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe' last_name, 12 balance, to_date('02/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe' last_name, 1 balance, to_date('03/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe' last_name, 16 balance, to_date('04/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe' last_name, 16 balance, to_date('05/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe' last_name, 16 balance, to_date('06/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe2' last_name, 1600 balance, to_date('01/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe2' last_name, 12 balance, to_date('02/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe2' last_name, 1 balance, to_date('03/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe2' last_name, 16 balance, to_date('04/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe2' last_name, 15 balance, to_date('05/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe2' last_name, 16 balance, to_date('06/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe2' last_name, 16 balance, to_date('07/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe3' last_name, 1600 balance, to_date('01/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe3' last_name, 12 balance, to_date('02/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe3' last_name, 1 balance, to_date('03/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe3' last_name, 16 balance, to_date('04/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe3' last_name, 16 balance, to_date('05/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe3' last_name, 16 balance, to_date('06/01/2017', 'dd/mm/yyyy') update_date FROM dual UNION ALL
                          SELECT 'John' first_name, 'Doe3' last_name, 17 balance, to_date('07/01/2017', 'dd/mm/yyyy') update_date FROM dual)
SELECT first_name,
       last_name,
       balance,
       min(update_date) update_date
FROM   (SELECT first_name,
               last_name,
               balance,
               update_date,
               row_number() OVER (PARTITION BY first_name, last_name ORDER BY update_date DESC) -- row number across the entire dataset (i.e. for each first_name and last_name)
                 - row_number() OVER (PARTITION BY first_name, last_name, balance ORDER BY update_date DESC) grp -- row number across each balance in the entire dataset.
        FROM   customer_balance)
WHERE  grp = 0
GROUP BY first_name,
         last_name,
         balance;

FIRST_NAME LAST_NAME    BALANCE UPDATE_DATE
---------- --------- ---------- -----------
John       Doe               16 04/01/2017
John       Doe2              16 06/01/2017
John       Doe3              17 07/01/2017

我提供了3个场景:

  1. 最新行用于相同的余额,但该数据集中较早出现的余额(即原始数据集)
  2. 最新的行用于相同的余额,但该余额在数据集中较早出现
  3. 最新一行与前一行的余额不同。

0
投票

也许你可以尝试这个查询

WITH bal AS
  (SELECT 'John' first_name,
                 'Doe' last_name,
                       1600 balance,
                       to_date('20170101', 'YYYYMMDD') update_date
   FROM dual
   UNION ALL SELECT 'John',
                    'Doe',
                    12 balance,
                    to_date('20170102', 'YYYYMMDD') update_date
   FROM dual
   UNION ALL SELECT 'John',
                    'Doe',
                    1 balance,
                    to_date('20170103', 'YYYYMMDD') update_date
   FROM dual
   UNION ALL SELECT 'John',
                    'Doe',
                    16 balance,
                    to_date('20170104', 'YYYYMMDD') update_date
   FROM dual
   UNION ALL SELECT 'John',
                    'Doe',
                    16 balance,
                    to_date('20170105', 'YYYYMMDD') update_date
   FROM dual
   UNION ALL SELECT 'John',
                    'Doe',
                    16 balance,
                    to_date('20170106', 'YYYYMMDD') update_date
   FROM dual
   UNION ALL SELECT 'John',
                    'Doe',
                    328 balance,
                    to_date('20170107', 'YYYYMMDD') update_date
   FROM dual) -- The main query

SELECT *
FROM
  (SELECT bal.*,
          LAG(balance) OVER(PARTITION BY first_name, last_name
                            ORDER BY update_date)prev_balance
   FROM bal )
WHERE prev_balance IS NULL
  OR balance != prev_balance

在第一步中,我们获得之前的余额。在第二个,我们删除前一个余额等于当前余额的所有行。 BTW对不起我从智能手机上回答的布局。


0
投票

我没有时间写出经过测试的解决方案,但分析函数lead()lag()的目的是:

select name, last_name, balance, update_date
  from (select name,
               last_name,
               balance,
               update_date,
               lead(balance) over (partition by first_name, last_name
                                      order by update_date) 
                 as next_balance
         where balance = :target_balance
         order by update_date
       )
 where balance <> next_balance
   and rownum = 1
© www.soinside.com 2019 - 2024. All rights reserved.