订单后的RANK / GROUP结果

时间:2017-03-16 22:25:47

标签: sql tsql gaps-and-islands

我有SQL查询,我正在尝试对订购后显示的结果进行RANK / GROUP。

这是针对从一个存储位置(SLOC)到另一个存储位置(SLOC)的情况(UNIQSN)。我试图按时间顺序获取存储位置之间的历史记录。

“代码”胜过千言万语!

我尝试了以下查询:

SELECT 
  uniqsn,
  SLOC,
  Update_DATETIME,
  RANK() OVER (PARTITION BY SLOC ORDER BY Update_DATETIME) RANKING
FROM TABLE

我得到了

UNIQSN  SLOC    UPDATE_DATETIME RANKING
6039133 C114    2014/10/13 16:35:18 1
6039133 C114    2015/02/23 07:58:22 2
6039133 C119    2014/09/23 20:57:30 1
6039133 C119    2014/09/23 20:57:57 2
6039133 C119    2014/09/25 08:11:19 3
6039133 C119    2015/01/29 17:39:50 4
6039133 C119    2015/01/29 17:42:02 5
6039133 C119    2015/01/30 09:01:02 6
6039133 C119    2017/03/04 09:46:21 7
6039133 C119    2017/03/04 09:46:28 8
6039133 C119    2017/03/09 07:18:27 9
6039133 C11M    2014/09/25 08:11:19 1
6039133 C11M    2014/10/13 12:11:44 2
6039133 C11M    2014/10/13 16:35:17 3
6039133 C11M    2014/10/14 07:58:59 4
6039133 C11M    2014/10/14 07:59:27 5
6039133 C11M    2014/10/14 08:03:06 6
6039133 C11M    2015/01/30 09:01:19 7
6039133 C11M    2015/02/20 14:08:37 8
6039133 C11M    2015/02/23 07:58:21 9
6039133 C11M    2017/03/09 07:18:28 10
6039133 C11Z    2014/10/14 08:03:07 1

相反,我试图实现以下结果。任何人都可以帮忙吗?

UNIQSN  SLOC    UPDATE_DATETIME     RANKING
6039133 C119    2014/09/23 20:57:30     1
6039133 C119    2014/09/23 20:57:57     1
6039133 C119    2014/09/25 08:11:19     1
6039133 C11M    2014/09/25 08:11:19     2
6039133 C11M    2014/10/13 12:11:44     2
6039133 C11M    2014/10/13 16:35:17     2
6039133 C114    2014/10/13 16:35:18     3
6039133 C11M    2014/10/14 07:58:59     4
6039133 C11M    2014/10/14 07:59:27     4
6039133 C11M    2014/10/14 08:03:06     4
6039133 C11Z    2014/10/14 08:03:07     5
6039133 C119    2015/01/29 17:39:50     6
6039133 C119    2015/01/29 17:42:02     6
6039133 C119    2015/01/30 09:01:02     6
6039133 C11M    2015/01/30 09:01:19     7
6039133 C11M    2015/02/20 14:08:37     7
6039133 C11M    2015/02/23 07:58:21     7
6039133 C114    2015/02/23 07:58:22     8
6039133 C119    2017/03/04 09:46:21     9
6039133 C119    2017/03/04 09:46:28     9
6039133 C119    2017/03/09 07:18:27     9
6039133 C11M    2017/03/09 07:18:28     10

谢谢!

2 个答案:

答案 0 :(得分:1)

你有差距和岛屿问题。此外,您希望按照最早的日期订购这些组(我可以告诉您)。

为此目的:

select t.*, dense_rank() over (partition by uniqsqn order by min_ud) as ranking               
from (select t.*,
             min(update_datetime) over (partition by uniqsqn, sloc, seqnum - seqnum_us) as min_ud
      from (select t.*,
                   row_number() over (partition by unisqn order by update_datetime) as seqnum,
                   row_number() over (partition by uniqsqn, sloc order by update_datetime) as seqnum_us
            from t
           ) t
     ) t;

答案 1 :(得分:0)

;With Cte1
AS
(     
SELECT '6039133' AS UNIQSN, 'C114' AS SLOC,   '2014/10/13 16:35:18' AS UPDATE_DATETIME UNION ALL
SELECT '6039133', 'C114',   '2015/02/23 07:58:22'                                      UNION ALL
SELECT '6039133', 'C119',   '2014/09/23 20:57:30'                                      UNION ALL
SELECT '6039133', 'C119',   '2014/09/23 20:57:57'                                      UNION ALL
SELECT '6039133', 'C119',   '2014/09/25 08:11:19'                                      UNION ALL
SELECT '6039133', 'C119',   '2015/01/29 17:39:50'                                      UNION ALL
SELECT '6039133', 'C119',   '2015/01/29 17:42:02'                                      UNION ALL
SELECT '6039133', 'C119',   '2015/01/30 09:01:02'                                      UNION ALL
SELECT '6039133', 'C119',   '2017/03/04 09:46:21'                                      UNION ALL
SELECT '6039133', 'C119',   '2017/03/04 09:46:28'                                      UNION ALL
SELECT '6039133', 'C119',   '2017/03/09 07:18:27'                                      UNION ALL
SELECT '6039133', 'C11M',   '2014/09/25 08:11:19'                                      UNION ALL
SELECT '6039133', 'C11M',   '2014/10/13 12:11:44'                                      UNION ALL
SELECT '6039133', 'C11M',   '2014/10/13 16:35:17'                                      UNION ALL
SELECT '6039133', 'C11M',   '2014/10/14 07:58:59'                                      UNION ALL
SELECT '6039133', 'C11M',   '2014/10/14 07:59:27'                                      UNION ALL
SELECT '6039133', 'C11M',   '2014/10/14 08:03:06'                                      UNION ALL
SELECT '6039133', 'C11M',   '2015/01/30 09:01:19'                                      UNION ALL
SELECT '6039133', 'C11M',   '2015/02/20 14:08:37'                                      UNION ALL
SELECT '6039133', 'C11M',   '2015/02/23 07:58:21'                                      UNION ALL
SELECT '6039133', 'C11M',   '2017/03/09 07:18:28'                                      UNION ALL
SELECT '6039133', 'C11Z',   '2014/10/14 08:03:07'
)
,Cte2
AS
(
SELECT 
  uniqsn,
  SLOC,
  Update_DATETIME,
  RANK() OVER (PARTITION BY SLOC ORDER BY Update_DATETIME) RANKING
FROM Cte1
)
SELECT * from Cte2 Order by RANKING