SQL / HIVE - 解决问题(小改动) - 分区

时间:2017-07-19 00:31:07

标签: sql sql-server hiveql

我有以下代码,这些代码主要适用于我测试过的其他一些场景。

但是,对于下面的例子,我试图计算N-CO(非完成)的数量直到完成,但它返回5而不是3。

我正在尝试创建另一列来计算PARTIALLY,即2。

任何一次都有一些输入,为什么? 我需要改变什么?

PROBLEM

CREATE TABLE #temp
(
Identifier varchar(20)NOT NULL
,CreatedDate DATETIME NOT NULL
,CompletedDate DATETIME NOT NULL
,SN_Type varchar(20) NOT NULL
,SN_Status varchar(20) NOT NULL
)
;
INSERT INTO #temp
VALUES('64074558792','20160729','20160805','Re-Activattion','N-CO');
INSERT INTO #temp
VALUES('64074558792','20160729','20160805','Re-Activattion','PARTIALLY');
INSERT INTO #temp
VALUES('64074558792','20160809','20160809','Re-Activattion','PARTIALLY');
INSERT INTO #temp
VALUES('64074558792','20160810','20160810','Re-Activattion','N-CO');
INSERT INTO #temp
VALUES('64074558792','20160812','20160812','Re-Activattion','N-CO');
INSERT INTO #temp
VALUES('64074558792','20160811','20160811','Re-Activattion','COMP');
INSERT INTO #temp
VALUES('64074558792','20160811','20160813','Re-Activattion','N-CO');
;

WITH Src AS (
SELECT Identifier, CreatedDate, CompletedDate, SN_Type, SN_Status,
 ROW_NUMBER() OVER(PARTITION BY Identifier ORDER BY CreatedDate, CASE WHEN SN_Status = 'COMP' THEN 1 ELSE 0 END, CompletedDate) AS rn,
 ROW_NUMBER() OVER(PARTITION BY Identifier ORDER BY CreatedDate, CASE WHEN SN_Status = 'COMP' THEN 1 ELSE 0 END, CompletedDate) -
 ROW_NUMBER() OVER(PARTITION BY Identifier,CASE WHEN SN_Status = 'COMP' THEN 1 ELSE 0 END ORDER BY CreatedDate, CompletedDate) AS grp
FROM #temp
),
Grouped AS (
SELECT Identifier, CASE WHEN SN_Status = 'COMP' THEN 1 ELSE 0 END AS IsCOMP,
 MIN(CreatedDate) AS StartDate,
 COUNT(*) AS [RE-AN NCO #],
 MAX(rn) AS LastRn 
FROM Src
GROUP BY Identifier, CASE WHEN SN_Status = 'COMP' THEN 1 ELSE 0 END, grp
),
grouped2 AS (SELECT Identifier, MAX(rn) AS maxRN
FROM [Src]

GROUP BY [Src].[Identifier])

SELECT s.Identifier,
    CASE WHEN isComp = 0
    THEN
        CAST(DATEDIFF(day,g.StartDate,s.CreatedDate) AS VARCHAR(25))
    ELSE
        'NOT COMPLETED'
    END AS RE_ACT_COMPLETION_TIME,
 g.[RE-AN NCO #]
FROM Src s
INNER JOIN Grouped g ON g.Identifier = s.Identifier
  AND g.LastRn + 1 = s.rn
JOIN grouped2 g2 ON [g2].[Identifier] = [s].[Identifier]
WHERE s.SN_Status = 'COMP'
    OR (SN_Status <> 'COMP' AND maxRN = [s].[rn])

ORDER BY rn;
DROP TABLE #temp

1 个答案:

答案 0 :(得分:0)

select  Identifier
       ,COMP_id
       ,count(case when SN_Status = 'N-CO'      then 1 end) as count_N_CO
       ,count(case when SN_Status = 'PARTIALLY' then 1 end) as count_PARTIALLY
       ,count(case when SN_Status = 'COMP'      then 1 end) as is_COMP

from   (select  Identifier
               ,SN_Status
               ,count(case when SN_Status = 'COMP' then 1 end) over
                (
                    partition by    Identifier 
                    order by        CreatedDate, case when SN_Status = 'COMP' then 1 else 0 end, CompletedDate
                    rows            between unbounded preceding and 1 preceding
                ) + 1   as COMP_id

        from    #temp
        ) t

group by    Identifier
           ,COMP_id
+-------------+---------+------------+-----------------+---------+
| Identifier  | COMP_id | count_N_CO | count_PARTIALLY | is_COMP |
+-------------+---------+------------+-----------------+---------+
| 64074558792 |       1 |          3 |               2 |       1 |
| 64074558792 |       2 |          1 |               0 |       0 |
+-------------+---------+------------+-----------------+---------+