Oracle按组划分为基于日期的序列

时间:2013-03-07 07:50:50

标签: sql oracle analytic-functions

我正在尝试使用PARTITION BY OVER按特定列“分组”行。我有点理解PARTITION的使用,但是我想按日期“阻止”分区。例如,如果我们有

|col1|col2       |
| A  |01/JAN/2012|
| A  |01/FEB/2012|
| B  |01/MAR/2012|
| B  |01/APR/2012|
| A  |01/MAY/2012|

我希望按col1进行分区,但我希望最后一个A与前两个A“不同”,因为它的日期分隔为'B'行。

如果我使用;

SELECT ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col2) AS RNUM, a.* 
FROM table1 a;

它会屈服;

|RNUM|col1|col2       |
|   1| A  |01/JAN/2012|
|   2| A  |01/FEB/2012|
|   3| A  |01/MAY/2012|
|   1| B  |01/MAR/2012|
|   2| B  |01/APR/2012|

但我真正想要的是;

|RNUM|col1|col2       |
|   1| A  |01/JAN/2012|
|   2| A  |01/FEB/2012|
|   1| B  |01/MAR/2012|
|   2| B  |01/APR/2012|
|   1| A  |01/MAY/2012|

这是否可以使用PARTITION BY OVER?目前我已经退回到使用游标来解析数据并分配一个组ID,这样我就可以分开两个'A'序列,但这很慢。

谢谢,

标记。

4 个答案:

答案 0 :(得分:4)

这可以通过几个分析来实现:

select col1, col2, row_number() over (partition by grp order by col2) rnum
  from (select col1, col2, max(grp) over(order by col2) grp
          from (select col1, col2, 
                       case 
                         when lag(col1) over (order by col2) != col1
                         then
                           row_number() over (order by col2)
                         when row_number() over(order by col2) = 1 
                         then
                           1
                       end grp
                  from data));

即:

首先获取col1col2日期更改排序的边界:

SQL> select col1, col2,
  2         case
  3           when lag(col1) over (order by col2) != col1
  4           then
  5             row_number() over (order by col2)
  6           when row_number() over(order by col2) = 1
  7           then
  8             1
  9         end grp
 10    from data;

C COL2             GRP
- --------- ----------
A 01-JAN-12          1
A 01-FEB-12
B 01-MAR-12          3
B 01-APR-12
A 01-MAY-12          5

然后我们可以填写这些空值:

SQL> select col1, col2, max(grp) over(order by col2) grp
  2    from (select col1, col2,
  3                  case
  4                    when lag(col1) over (order by col2) != col1
  5                    then
  6                      row_number() over (order by col2)
  7                    when row_number() over(order by col2) = 1
  8                    then
  9                      1
 10                  end grp
 11            from data);

C COL2             GRP
- --------- ----------
A 01-JAN-12          1
A 01-FEB-12          1
B 01-MAR-12          3
B 01-APR-12          3
A 01-MAY-12          5

然后通过按row_number()排序并在col2

上进行分区来分配grp

小提琴:http://sqlfiddle.com/#!4/4818c/1

答案 1 :(得分:0)

首先,你应该找到每条记录的GROUP_ID,以便将所有类似的COL1排序到不同的组,如果它们之间有差距的话。然后在带有COL1的OVER语句中使用此GROUP_ID:

SQLFiddle demo

SELECT ROW_NUMBER() OVER (PARTITION BY Group_id,col1 ORDER BY col2) AS RNUM, a3.* 
FROM 
(
select a1.*,
      (select count(*) from t a2 where 
       a2.col1<>a1.col1 
       AND  
       a2.col2<a1.col2) as GROUP_ID
from t a1
) a3

order by col2

答案 2 :(得分:0)

请参阅下面的方法,它类似于Dazzal的答案,有点不同的逻辑:

SQL FIDDLE

步骤1:

--find the swhitches to new groups
select col1, col2, 
    case when nvl(lag(col1) over (order by col2),sysdate) <> col1 then 1 end as new_grp
  from data;

COL1    COL2        NEW_GRP
A   January, 01 2012    1
A   February, 01 2012   (null)
B   March, 01 2012      1
B   April, 01 2012      (null)
A   May, 01 2012        1

步骤2:

--identify/mark the groups

select col1, col2, sum(new_grp) over (order by col2) as grp
from(
  select col1, col2, 
    case when nvl(lag(col1) over (order by col2),sysdate) <> col1 then 1 end as new_grp
  from data)
  ;

COL1    COL2        NEW_GRP
A   January, 01 2012    1
A   February, 01 2012   1
B   March, 01 2012      2
B   April, 01 2012      2
A   May, 01 2012        3

步骤3:

--find the row_number within group
select col1, col2, row_number() over(partition by grp order by col2) rn
from(
  select col1, col2, sum(new_grp) over (order by col2) as grp
  from(
    select col1, col2, 
      case when nvl(lag(col1) over (order by col2),sysdate) <> col1 then 1 end as new_grp
    from data
      )
  );

COL1    COL2        NEW_GRP
A   January, 01 2012    1
A   February, 01 2012   2
B   March, 01 2012      1
B   April, 01 2012      2
A   May, 01 2012        1

答案 3 :(得分:0)

您不需要分区。您需要将日期转换为DD / MM / YYYY格式并订购。或者,如果必须,那么你可以通过MM部分进行分区,它可以给你01,02,03 ...并且可以根据需要进行分区并轻松转换为数字。但是你不需要这一切......不要让你的查询复杂化。始终保持简单。外部查询仅用于将日期重新格式化为DD / MON / YYYY格式:

SELECT val, to_char(to_date(dt, 'DD/MM/YYYY'), 'DD/MON/YYYY') formatted_date 
  FROM
( -- Format your date to DD/MM/YYYY and order by it --
SELECT 'A' val, to_char(to_date('01/JAN/2012'), 'DD/MM/YYYY') dt FROM dual  
 UNION
SELECT 'A', to_char(to_date('01/FEB/2012'), 'DD/MM/YYYY') FROM dual  
 UNION
SELECT 'B',to_char(to_date('01/MAR/2012'), 'DD/MM/YYYY') FROM dual  
 UNION
SELECT 'B',to_char(to_date('01/APR/2012'), 'DD/MM/YYYY') FROM dual  
 UNION
SELECT 'A',to_char(to_date('01/MAY/2012'), 'DD/MM/YYYY') FROM dual  
ORDER BY 2
)
/

您的日期按照您的要求订购:

VAL FORMATTED_DATE
-------------------
A   01/JAN/2012
A   01/FEB/2012
B   01/MAR/2012
B   01/APR/2012
A   01/MAY/2012