我正在尝试确定在其他组之后的记录组。我们的成员离开了一个小组,然后又回来了,但不应该再回到另一个小组。
我正在尝试确定成员返回的月份-首次注册后。我试图以各种方式(Row_Num,排名,分组)对它们进行分组,但还没有弄清楚。
我想确定突出显示的月份,在该月份中,成员资格消失。为了使其更具挑战性,一些成员开始时就没有资格(0、1、0、1)。我想确定按月划分的 Enrolled = 1 的第二组和后续组-可能通过添加其他列来对更改进行分组,例如下面的BLUE中的列。
这是一些示例数据。关于如何进行这项工作的任何其他建议,将不胜感激。
CREATE TABLE #TEMP(MEMBER VARCHAR(20), MDATE DATE, ELIGIBLE INT, ENROLLED INT)
INSERT INTO #TEMP ( MEMBER, MDATE, ELIGIBLE, ENROLLED )
SELECT 'JUDY' AS MEMBER, '2017-01-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2017-02-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2017-03-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2017-04-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2017-05-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2017-06-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2017-07-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2017-08-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2017-09-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2017-10-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2017-11-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2017-12-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2018-01-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2018-02-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2018-03-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2018-04-01' AS MDATE, 1 AS ELIGIBLE, 1 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2018-05-01' AS MDATE, 1 AS ELIGIBLE, 1 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2018-06-01' AS MDATE, 1 AS ELIGIBLE, 1 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2018-07-01' AS MDATE, 1 AS ELIGIBLE, 1 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2018-08-01' AS MDATE, 1 AS ELIGIBLE, 1 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2018-09-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2018-10-01' AS MDATE, 0 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2018-11-01' AS MDATE, 1 AS ELIGIBLE, 1 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2018-12-01' AS MDATE, 1 AS ELIGIBLE, 1 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2019-01-01' AS MDATE, 1 AS ELIGIBLE, 1 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2019-02-01' AS MDATE, 1 AS ELIGIBLE, 1 AS ENROLLED UNION
SELECT 'JUDY' AS MEMBER, '2019-03-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2017-01-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2017-02-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2017-03-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2017-04-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2017-05-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2017-06-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2017-07-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2017-08-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2017-09-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2017-10-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2017-11-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2017-12-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2018-01-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2018-02-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2018-03-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2018-04-01' AS MDATE, 1 AS ELIGIBLE, 1 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2018-05-01' AS MDATE, 1 AS ELIGIBLE, 1 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2018-06-01' AS MDATE, 1 AS ELIGIBLE, 1 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2018-07-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2018-08-01' AS MDATE, 0 AS ELIGIBLE, 0 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2018-09-01' AS MDATE, 1 AS ELIGIBLE, 1 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2018-10-01' AS MDATE, 1 AS ELIGIBLE, 1 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2018-11-01' AS MDATE, 1 AS ELIGIBLE, 1 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2018-12-01' AS MDATE, 1 AS ELIGIBLE, 1 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2019-01-01' AS MDATE, 1 AS ELIGIBLE, 1 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2019-02-01' AS MDATE, 1 AS ELIGIBLE, 1 AS ENROLLED UNION
SELECT 'FRANK' AS MEMBER, '2019-03-01' AS MDATE, 1 AS ELIGIBLE, 0 AS ENROLLED
答案 0 :(得分:2)
我不确定您是什么意思,“有些成员一开始就不合格(0,1,0,1)”,但是基于您想要的输出:
WITH TEMP2(MEMBER, MDATE, ELIGIBLE, ENROLLED, CHANGED) AS (
SELECT MEMBER, MDATE, ELIGIBLE, ENROLLED,CASE WHEN ELIGIBLE=(LAG(ELIGIBLE) OVER (PARTITION BY MEMBER ORDER BY MEMBER, MDATE)) THEN 0 ELSE 1 END CHANGED
FROM #TEMP
)
SELECT MEMBER, MDATE, ELIGIBLE, ENROLLED, SUM(CHANGED) OVER (PARTITION BY 1 ORDER BY MEMBER, MDATE) QUESTIONMARK
FROM TEMP2