Oracle SQL查询对连续记录进行分组 [英] Oracle SQL query to group consecutive records

查看:577
本文介绍了Oracle SQL查询对连续记录进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将电子表格中的数据(金额"和叙述")导入表中,并且需要查询的帮助,以便根据连续记录的叙述"对它们进行分组,例如:

I've imported data ("Amount" and "Narration") from a spreadsheet into a table and need help with a query to group consecutive records according to their "Narration", for example:

预期输出:

line_no  amount   narration   calc_group <-Not part of table
----------------------------------------
1          10      Reason 1       1
2         -10      Reason 1       1
3           5      Reason 2       2
4           5      Reason 2       2
5         -10      Reason 2       2
6          -8      Reason 1       3
7           8      Reason 1       3
8          11      Reason 1       3
9          99      Reason 3       4
10        -99      Reason 3       4

我已经尝试了一些分析功能:

I've tried some analytical functions:

select   line_no, amount, narration,
         first_value (line_no) over 
         (partition by narration order by line_no) "calc_group"
from     test
order by line_no

但这不起作用,因为第6至8行的旁白与第1行和第2行相同.

But that does not work because the Narration of line 6 to 8 is the same as line 1 and 2.

line_no  amount   narration   calc_group
----------------------------------------
1          10      Reason 1       1
2         -10      Reason 1       1
3           5      Reason 2       3
4           5      Reason 2       3
5         -10      Reason 2       3
6          -8      Reason 1       1
7           8      Reason 1       1
8          11      Reason 1       1
9          99      Reason 3       4
10        -99      Reason 3       4

更新

我已经设法使用滞后分析功能和序列来做到这一点,虽然不是很优雅,但是它可以工作.应该有更好的方法,请发表评论!

UPDATE

I've managed to do it using lag analytical function and sequences, not very elegant but it works. There should be a better way, please comment!

create or replace function get_next_test_seq
   return number
as
begin
   return test_seq.nextval;
end get_next_test_seq;

create or replace function get_curr_test_seq
   return number
as
begin
   return test_seq.currval;
end get_curr_test_seq;

update test
set    group_no =
          (with cte1
                as (select   line_no, amount, narration,
                             lag (narration) over (order by line_no) prev_narration, group_no
                    from     test
                    order by line_no),
                cte2
                as (select line_no, amount, narration, group_no,
                           case when prev_narration is null or prev_narration <> narration then get_next_test_seq else get_curr_test_seq end new_group_no
                    from   cte1)
           select new_group_no
           from   cte2
           where  cte2.line_no = test.line_no);

更新2

我对更好的答案感到满意.谢谢kordiko!

UPDATE 2

I'm satisfied with the better accepted answer. Thanks kordiko!

推荐答案

尝试以下查询:

SELECT line_no,
       amount,
       narration,
       SUM( x ) OVER ( ORDER BY line_no
                       ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 
       ) as calc_group
FROM (
  SELECT t.*,
         CASE lag( narration ) OVER (order by line_no ) 
         WHEN narration THEN 0
         ELSE 1 END x
  FROM test t

)
ORDER BY line_no

演示-> http://www.sqlfiddle.com/#!4/6d7aa/9

这篇关于Oracle SQL查询对连续记录进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆