我在SQL中有一个摘录,我想按照下表所示的格式对所需输出列中的数据进行分组/排名。我的问题是如何实现这一目标。我已经尝试过Rank和Dense rank功能,但它没有给我想要的结果。
Cusomter_Id | Rno | Start_Dt | End_Dt | PM_Status | Desired_Output
-------------------------------------------------- -----------------------
1609 | 1 | 02/10/2014 | 08/10/2014 | VD | 1
-------------------------------------------------- -----------------------
1609 | 2 | 09/10/2014 | 02/11/2014 | CC | 2
-------------------------------------------------- -----------------------
1609 | 3 | 03/11/2014 | 04/11/2014 | CC | 2
-------------------------------------------------- -----------------------
1609 | 4 | 05/11/2014 | 27/01/2015 | FD | 3
-------------------------------------------------- -----------------------
1609 | 5 | 28/01/2015 | 02/02/2015 | CC | 4
-------------------------------------------------- -----------------------
1609 | 6 | 03/02/2015 | 09/02/2015 | CC | 4
-------------------------------------------------- -----------------------
1609 | 7 | 10/02/2015 | 31/12/9999 | CC | 4
-------------------------------------------------- -----------------------
我希望上述数据有意义,请告诉我。
非常感谢您查看我的查询。
答案 0 :(得分:1)
您可以使用递归CTE查找分组并获得所需结果:
DECLARE @Data TABLE (
Customer_Id INT,
Rno INT,
Start_Dt DATE,
End_Dt DATE,
PM_Status VARCHAR(5),
Desired INT
)
INSERT @Data VALUES
(1609, 1, '2/10/2014', '10/8/2014', 'VD', 1),
(1609, 2, '10/9/2014', '11/2/2014', 'CC', 2),
(1609, 3, '11/3/2014', '11/4/2014', 'CC', 2),
(1609, 4, '11/5/2014', '1/27/2014', 'FD', 3),
(1609, 5, '1/28/2015', '2/2/2015', 'CC', 4),
(1609, 6, '2/3/2015', '2/9/2015', 'CC', 4),
(1609, 7, '2/10/2015', NULL, 'CC', 4)
; WITH CTE AS (
SELECT
Customer_id,
Rno,
Start_Dt,
End_Dt,
PM_Status,
Desired,
1 AS Actual
FROM @Data
WHERE RNo = 1
UNION ALL
SELECT
SRC.Customer_id,
SRC.Rno,
SRC.Start_Dt,
SRC.End_Dt,
SRC.PM_Status,
SRC.Desired,
CASE WHEN CTE.PM_Status <> SRC.PM_Status
THEN CTE.Actual + 1 ELSE CTE.Actual END
FROM CTE
INNER JOIN @Data SRC
ON SRC.Customer_Id = CTE.Customer_Id
AND SRC.Rno = CTE.Rno + 1
)
SELECT *
FROM CTE
输出:
Customer_id Rno Start_Dt End_Dt PM_Status Desired Actual
----------- ----------- ---------- ---------- --------- ----------- -----------
1609 1 2014-02-10 2014-10-08 VD 1 1
1609 2 2014-10-09 2014-11-02 CC 2 2
1609 3 2014-11-03 2014-11-04 CC 2 2
1609 4 2014-11-05 2014-01-27 FD 3 3
1609 5 2015-01-28 2015-02-02 CC 4 4
1609 6 2015-02-03 2015-02-09 CC 4 4
1609 7 2015-02-10 NULL CC 4 4