使用分区无法获得行号正确

时间:2017-03-03 12:49:35

标签: sql window-functions

我正在尝试让这个查询以某种方式输出。基本上这是我得到的输出:

+-------+------------+-------------+----------+--------+
| Col_A |    Date    |  Subscr_Id  | Segment  | RowNum |
+-------+------------+-------------+----------+--------+
|     1 | 16/09/2008 | 5,954,766   | UNKNOWN  |      1 |
|     2 | 17/09/2008 | 5,954,766   | UNKNOWN  |      2 |
|     3 | 24/09/2008 | 5,954,766   | UNKNOWN  |      3 |
|     4 | 25/09/2008 | 5,954,766   | UNKNOWN  |      4 |
|     5 | 24/06/2009 | 5,954,766   | CONSUMER |      1 |
|     6 | 01/09/2009 | 5,954,766   | CONSUMER |      2 |
|     7 | 09/11/2009 | 5,954,766   | CONSUMER |      3 |
|     8 | 31/12/2009 | 5,954,766   | CONSUMER |      4 |
|     9 | 01/01/2010 | 5,954,766   | CONSUMER |      5 |
|    10 | 31/01/2010 | 5,954,766   | CONSUMER |      6 |
|    11 | 28/02/2010 | 5,954,766   | CONSUMER |      7 |
|    12 | 01/04/2010 | 5,954,766   | CONSUMER |      8 |
|    13 | 28/06/2010 | 5,954,766   | UNKNOWN  |      5 |
|    14 | 14/11/2010 | 5,954,766   | UNKNOWN  |      6 |
|    15 | 09/06/2011 | 5,954,766   | UNKNOWN  |      7 |
|    16 | 22/09/2011 | 5,954,766   | UNKNOWN  |      8 |
|    17 | 18/12/2012 | 5,954,766   | UNKNOWN  |      9 |
|    18 | 12/02/2017 | 5,954,766   | UNKNOWN  |     10 |
|    19 | 14/02/2017 | 5,954,766   | UNKNOWN  |     11 |
|    20 | 20/02/2017 | 5,954,766   | UNKNOWN  |     12 |
|    21 | 16/09/2008 | 208,333,201 | ?        |      1 |
|    22 | 18/12/2012 | 208,333,201 | ?        |      2 |
|    23 | 12/02/2017 | 208,333,201 | CONSUMER |      1 |
|    24 | 13/02/2017 | 208,333,201 | CONSUMER |      2 |
|    25 | 14/02/2017 | 208,333,201 | CONSUMER |      3 |
|    26 | 15/02/2017 | 208,333,201 | CONSUMER |      4 |
|    27 | 20/02/2017 | 208,333,201 | CONSUMER |      5 |
|    28 | 28/02/2017 | 208,333,201 | CONSUMER |      6 |
+-------+------------+-------------+----------+--------+

每当RowNumSubscr_Id值发生变化时,我基本上希望Segment列重置为1。它几乎可以工作,但正如你在第十三行看到的那样,RowNum从5开始,而不是重置为1。

我正在使用的查询如下:

ROW_NUMBER() OVER (PARTITION BY Subscr_Id, Segment ORDER BY st.Subscr_Id, st.Effective_From_Dt) AS RowNum

有人能给我任何指示吗?

1 个答案:

答案 0 :(得分:0)

您需要识别相邻值,即使它们被其他值分隔。一种方法是使用行号的差异来识别相邻的行,然后使用row_number()

select t.*,
       row_number() over (partition by subscr_id, seqnum - seqnum_s
                          order by col_a
                         ) as rownum
from (select t.*,
             row_number() over (partition by subscr_id order by col_a) as seqnum,
             row_number() over (partition by subscr_id, segment order by col_a) as seqnum_s,
      from t
     ) t;

逻辑有点棘手。运行子查询以查看seqnum值是什么。您将看到差异如何识别您想要的组。