分区上的递归 CTE 或 ROW_NUMBER?

Posted

技术标签:

【中文标题】分区上的递归 CTE 或 ROW_NUMBER?【英文标题】:recursive CTE or ROW_NUMBER over partition? 【发布时间】:2018-05-21 11:35:08 【问题描述】:

您好,下面是一个小示例,我正在尝试找到一种方法来重置基于 CDO_OP_REFERRAL_UNIQUE_ID 的 ATTENDED_OR_DID_NOT_ATTEND 的 COUNT,最后一列是我似乎无法在查询中应用的预期结果,每次我使用 ROW_NUMBER分区我一直得到错误的结果,因为每次 ATTENDED_OR_DID_NOT_ATTEND 更改时它都不会再次重新启动

DECLARE @CDO_OP_APPOINTMENT TABLE (CDO_OP_REFERRAL_UNIQUE_ID int,LOCAL_PATIENT_NUMBER varchar(10),APPOINTMENT_START_DATE_TIME datetime,ATTENDED_OR_DID_NOT_ATTEND varchar(10),DESIRED_OUTCOME varchar(10))

INSERT INTO @CDO_OP_APPOINTMENT

VALUES

('480805568',   'HEY1030785',   '05/11/2013 10:00', '2',    '1'),
('480805568',   'HEY1030785',   '12/11/2013 10:00', '5',    '1'),
('480805568',   'HEY1030785',   '22/11/2013 09:30', '5',    '2'),
('480805568',   'HEY1030785',   '03/12/2013 13:00', '3',    '1'),
('480805568',   'HEY1030785',   '30/12/2013 10:15', '5',    '1'),
('480805568',   'HEY1030785',   '24/02/2014 09:15', '4',    '1'),
('480805568',   'HEY1030785',   '24/02/2014 14:15', '5',    '1'),
('480805568',   'HEY1030785',   '17/03/2014 15:25', '4',    '1'),
('480805568',   'HEY1030785',   '20/03/2014 18:50', '5',    '1'),
('480805568',   'HEY1030785',   '23/09/2014 15:55', '5',    '2'),
('480805568',   'HEY1030785',   '14/04/2015 16:30', '5',    '3'),
('480805568',   'HEY1030785',   '14/04/2015 17:30', '4',    '1'),
('480805568',   'HEY1030785',   '15/05/2015 14:15', '5',    '1')

SELECT * from @CDO_OP_APPOINTMENT

【问题讨论】:

指定预期结果。并标记 dbms。 最后一列是我想要的结果,它在 SQL 中复制它我无法解决 @Simon,你在使用 SQL Server 吗?什么版本? @Simon 。 . .阅读“分区”标签的含义。我删除了它。 【参考方案1】:

这是一个孤岛问题。最简单的解决方案是行号不同:

select a.*,
       row_number() over (partition by CDO_OP_REFERRAL_UNIQUE_ID, ATTENDED_OR_DID_NOT_ATTEND , seqnum - seqnum_2 order by APPOINTMENT_START_DATE_TIME) as desired_result
from (select a.*,
             row_number() over (partition by CDO_OP_REFERRAL_UNIQUE_ID order by APPOINTMENT_START_DATE_TIME) as seqnum,
             row_number() over (partition by CDO_OP_REFERRAL_UNIQUE_ID, ATTENDED_OR_DID_NOT_ATTEND order by APPOINTMENT_START_DATE_TIME) as seqnum_2,
      from @CDO_OP_APPOINTMENT a
     ) a;

【讨论】:

非常接近实际解决方案,因此也将标记为答案【参考方案2】:

设法解决与上面的答案非常相似

set dateformat dmy
go

DECLARE @CDO_OP_APPOINTMENT TABLE (CDO_OP_REFERRAL_UNIQUE_ID int,LOCAL_PATIENT_NUMBER varchar(10),APPOINTMENT_START_DATE_TIME datetime,ATTENDED_OR_DID_NOT_ATTEND varchar(10),DESIRED_OUTCOME varchar(10))

INSERT INTO @CDO_OP_APPOINTMENT

VALUES

('480805568',   'HEY1030785',   '05/11/2013 10:00', '2',    '1'),
('480805568',   'HEY1030785',   '12/11/2013 10:00', '5',    '1'),
('480805568',   'HEY1030785',   '22/11/2013 09:30', '5',    '2'),
('480805568',   'HEY1030785',   '03/12/2013 13:00', '3',    '1'),
('480805568',   'HEY1030785',   '30/12/2013 10:15', '5',    '1'),
('480805568',   'HEY1030785',   '24/02/2014 09:15', '4',    '1'),
('480805568',   'HEY1030785',   '24/02/2014 14:15', '5',    '1'),
('480805568',   'HEY1030785',   '17/03/2014 15:25', '4',    '1'),
('480805568',   'HEY1030785',   '20/03/2014 18:50', '5',    '1'),
('480805568',   'HEY1030785',   '23/09/2014 15:55', '5',    '2'),
('480805568',   'HEY1030785',   '14/04/2015 16:30', '5',    '3'),
('480805568',   'HEY1030785',   '14/04/2015 17:30', '4',    '1'),
('480805568',   'HEY1030785',   '15/05/2015 14:15', '5',    '1')


SELECT CDO_OP_REFERRAL_UNIQUE_ID ,LOCAL_PATIENT_NUMBER ,APPOINTMENT_START_DATE_TIME ,ATTENDED_OR_DID_NOT_ATTEND 
,ROW_NUMBER() OVER (PARTITION BY CDO_OP_REFERRAL_UNIQUE_ID,Seq1-Seq2,ATTENDED_OR_DID_NOT_ATTEND ORDER BY APPOINTMENT_START_DATE_TIME) AS DESIRED_OUTCOME 
FROM
(
SELECT *,ROW_NUMBER() OVER (PARTITION BY CDO_OP_REFERRAL_UNIQUE_ID ORDER BY APPOINTMENT_START_DATE_TIME) AS Seq1,
ROW_NUMBER() OVER (PARTITION BY CDO_OP_REFERRAL_UNIQUE_ID,ATTENDED_OR_DID_NOT_ATTEND ORDER BY APPOINTMENT_START_DATE_TIME) AS Seq2

 from @CDO_OP_APPOINTMENT
 )t
 ORDER BY APPOINTMENT_START_DATE_TIME

【讨论】:

以上是关于分区上的递归 CTE 或 ROW_NUMBER?的主要内容,如果未能解决你的问题,请参考以下文章

row_number 和 cte 使用实例:考场监考安排

查询性能:CTE 使用 ROW_NUMBER() 选择第一行

表数据子集上的递归 CTE

CTE、ROW_NUMBER 和 ROWCOUNT

在禁用分区功能的 SQL 中使用分区上的 Row_number

使用实体框架流利语法或内联语法编写递归 CTE