订购问题导致DENSE_RANK

时间:2011-04-27 09:58:37

标签: sql oracle

我希望有人可以帮助我解决我在Oracle SQL语句中使用DENSE_RANK作为分析函数的问题。我知道DENSE_RANK根据order_by_clause中的值计算每一行的等级

DATE        DEP_DT           LOG_NO LEG_NO   CREW                     DR
02/01/2011  02/01/2011 05:45    177 157511995    ANTONI,BARFAR,GARGNI   1
02/01/2011  02/01/2011 08:40    177 157512040    ANTONI,BARFAR,GARGNI   1
02/01/2011  02/01/2011 06:35    179 157556684    GGNCNI,HULBRA,LIFEDA   2
02/01/2011  02/01/2011 09:10    179 157556773    GGNCNI,HULBRA,LIFEDA   2
02/01/2011  02/01/2011 11:40    179 157534059    GGNCNI,HULBRA,LIFEDA   2
02/01/2011  02/01/2011 14:10    179 157533956    GGNCNI,HULBRA,LIFEDA   2
02/01/2011  02/01/2011 17:00    179 157542667    AINGHI,BOIGDE,MORWRE   1
02/01/2011  02/01/2011 19:55    179 157542712    AINGHI,BOIGDE,MORWRE   1

但是因为我使用工作人员计算排名,它按最低字母顺序的船员排序,但我真正需要的是按日期分组,log_no,船员然后按dep_dt排序如下:

DATE        DEP_DT           LOG_NO LEG_NO   CREW                     DR
02/01/2011  02/01/2011 05:45    177 157511995    ANTONI,BARFAR,GARGNI   1
02/01/2011  02/01/2011 08:40    177 157512040    ANTONI,BARFAR,GARGNI   1
02/01/2011  02/01/2011 06:35    179 157556684    GGNCNI,HULBRA,LIFEDA   1
02/01/2011  02/01/2011 09:10    179 157556773    GGNCNI,HULBRA,LIFEDA   1
02/01/2011  02/01/2011 11:40    179 157534059    GGNCNI,HULBRA,LIFEDA   1
02/01/2011  02/01/2011 14:10    179 157533956    GGNCNI,HULBRA,LIFEDA   1
02/01/2011  02/01/2011 17:00    179 157542667    AINGHI,BOIGDE,MORWRE   2
02/01/2011  02/01/2011 19:55    179 157542712    AINGHI,BOIGDE,MORWRE   2

 SELECT a.date, a.log_no, a.dep_dt, b.leg_no, b.crew,
 ,DENSE_RANK() OVER (PARTITION BY a.date, a.log_no ORDER BY b.crew) dr
 FROM leg a,
     (
        SELECT 
        leg_no, 
        WM_CONCAT(DISTINCT TRIM(code)) AS crew
        FROM leg_crew
        GROUP BY leg_no 
     ) b
 WHERE a.leg_no = b.leg_no 

我想这是一个比使用分析函数更好的方法,但我完全不知所措,所以任何建议都会非常感激!!

由于

1 个答案:

答案 0 :(得分:0)

我认为你可以通过这样的额外步骤来实现:

SQL> ALTER SESSION SET nls_date_format='dd/mm/yyyy hh24:mi';

Session altered

SQL> with my_data as (
  2  SELECT to_date('02/01/2011') my_date,  to_date('02/01/2011 05:45') DEP_DT, 177 LOG_NO, 157511995 LEG_NO, 'ANTONI,BARFAR,GARGNI' CREW FROM DUAL
  3  UNION ALL SELECT to_date('02/01/2011'),  to_date('02/01/2011 08:40'), 177, 157512040, 'ANTONI,BARFAR,GARGNI' FROM DUAL
  4  UNION ALL SELECT to_date('02/01/2011'),  to_date('02/01/2011 06:35'), 179, 157556684, 'GGNCNI,HULBRA,LIFEDA' FROM DUAL
  5  UNION ALL SELECT to_date('02/01/2011'),  to_date('02/01/2011 09:10'), 179, 157556773, 'GGNCNI,HULBRA,LIFEDA' FROM DUAL
  6  UNION ALL SELECT to_date('02/01/2011'),  to_date('02/01/2011 11:40'), 179, 157534059, 'GGNCNI,HULBRA,LIFEDA' FROM DUAL
  7  UNION ALL SELECT to_date('02/01/2011'),  to_date('02/01/2011 14:10'), 179, 157533956, 'GGNCNI,HULBRA,LIFEDA' FROM DUAL
  8  UNION ALL SELECT to_date('02/01/2011'),  to_date('02/01/2011 17:00'), 179, 157542667, 'AINGHI,BOIGDE,MORWRE' FROM DUAL
  9  UNION ALL SELECT to_date('02/01/2011'),  to_date('02/01/2011 19:55'), 179, 157542712, 'AINGHI,BOIGDE,MORWRE' FROM DUAL
 10  )
 11  SELECT my_date, dep_dt, log_no, leg_no, crew,
 12         dense_rank() over(PARTITION BY my_date, log_no ORDER BY min_dt, crew) dr
 13    FROM (SELECT d.*,
 14                 MIN(dep_dt) over(PARTITION BY my_date, log_no, crew) min_dt
 15            FROM my_data d);

MY_DATE     DEP_DT          LOG_NO     LEG_NO CREW                         DR
----------- ----------- ---------- ---------- -------------------- ----------
02/01/2011  02/01/2011         177  157512040 ANTONI,BARFAR,GARGNI          1
02/01/2011  02/01/2011         177  157511995 ANTONI,BARFAR,GARGNI          1
02/01/2011  02/01/2011         179  157556684 GGNCNI,HULBRA,LIFEDA          1
02/01/2011  02/01/2011         179  157556773 GGNCNI,HULBRA,LIFEDA          1
02/01/2011  02/01/2011         179  157533956 GGNCNI,HULBRA,LIFEDA          1
02/01/2011  02/01/2011         179  157534059 GGNCNI,HULBRA,LIFEDA          1
02/01/2011  02/01/2011         179  157542667 AINGHI,BOIGDE,MORWRE          2
02/01/2011  02/01/2011         179  157542712 AINGHI,BOIGDE,MORWRE          2

8 rows selected