我们可以在 sql 中使用超前和滞后 window 函数解决基本交易查询吗

can we solve basic transaction query using lead and lag window function in sql

我有这个i/ptable

balance trs_date
27000 2020-01-01
27000 2020-01-02
27000 2020-01-03
31000 2020-01-04
31000 2020-01-05
27000 2020-01-06
27000 2020-01-07
32000 2020-01-08
31000 2020-01-09

我想要这个o/p

balance s_date e_date
27000 2020-01-01 2020-01-03
31000 2020-01-04 2020-01-05
27000 2020-01-06 2020-01-07
32000 2020-01-08 2020-01-08
31000 2020-01-09 2020-01-09

我已经用 window 函数解决了它,比如 row_number 和分区

SELECT MIN(trdate) AS s_date,
      MAX(trdate)  AS e_date,
      t.balance
FROM   (
  SELECT t.*,
         ROW_NUMBER() OVER (ORDER BY trdate)
          - ROW_NUMBER() OVER (PARTITION BY balance ORDER BY trdate) AS grp
  FROM   bank t
) t
GROUP BY grp,t.balance order by s_date;

我答对了但是

问题是使用超前和滞后函数来解决这个问题,如果我们可以使用超前和滞后来解决这个问题,你能解释一下不熟悉超前和滞后函数的解决方案吗

你可以这样做:

select
  min(balance) as balance,
  min(trs_date) as s_date,
  max(trs_date) as e_date
from (
  select x.*, sum(inc) over(order by trs_date) as g
  from (
    select t.*, 
      case when balance <> lag(balance) over(order by trs_date)
           then 1 else 0 end as inc
    from t
  ) x
) y
group by g

解释:

  • 内部查询用 10 标记行,具体取决于它们是否开始一个新组。

  • 然后中间查询将这些增量求和以产生组号。

  • 最后,外部查询只使用组号和聚合值。