2017-01-24 8 views
0

私は可能な限りこれを置くでしょう。特定のステータスの後に同じIDの行を返す

私は、同じApplicationIDを持つ異なるApplicationStatusを持つ列を持っています。 ApplicationStatusが 'Audit Complete'に初めて到達したときにそれ以降にアクティビティが発生した場合、それらのApplicationStatusを返す必要があります( 'Audit Complete' ApplicationStatusの後に 'Audit Complete'が再度表示されても)。それはまた、それらの行を返すべきではありませんAPPLICATIONID年代のグループに表示され

My Table

これは私が(他の列には何の問題もありません)を返したいものです:。

  ApplicationID | Application Status 
      ----------------|-------------------- 
       926521  | Submitted to Bank 
       926521  | Audit Complete 

最初の「監査完了」ApplicationStatusは、ApplicationIDグループのどの行にも表示される可能性があります。

WITH CTE 
AS 
(
SELECT 
     status.ApplicationID AS [Application ID], 
     ROW_NUMBER() OVER(PARTITION BY status.ApplicationID ORDER BY status.ApplicationID) AS [Row Number], 
     status.ApplicationStatusMovementID AS [Application Status Movement ID], 
     status.ApplicationStatus AS [Application Status], 
     status.Comment, 
     status.CreatedDate AS [Created Date], 
     app.BondFacilitator AS [Bond Facilitator], 

     CASE 
     WHEN COUNT(*) OVER(PARTITION BY status.ApplicationID) > 3 AND status.ApplicationStatus IN ('Audit Complete') --OR COUNT(*) OVER(PARTITION BY status.ApplicationID) > 3 
     THEN 'NO' 
     WHEN COUNT(*) OVER(PARTITION BY status.ApplicationID) > 3 AND status.ApplicationStatus NOT IN ('Audit Complete') --OR COUNT(*) OVER(PARTITION BY status.ApplicationID) > 3 
     THEN 'FINE' 
     ELSE 'FINE' 
     END AS [Status] 

FROM Import.OobaApplicationStatusMovement AS status 

LEFT OUTER JOIN Import.OobaApplication AS app ON status.ApplicationID = app.ApplicationID 

GROUP BY 
     status.ApplicationID, 
     ApplicationStatusMovementID, 
     status.ApplicationStatus, 
     Comment, 
     status.CreatedDate, 
     BondFacilitator 

) 
SELECT [Application ID], 
     [Row Number], 
     [Application Status Movement ID], 
     [Application Status], 
     Comment, 
     [Created Date], 
     [Bond Facilitator], 
     [Status], 

     IIF(COUNT(*) OVER(PARTITION BY [Application ID]) > COUNT([Status]) OVER(PARTITION BY [Application ID], [Status]), 'NO', 'YES') AS [Activity After Audit Completed] 


FROM (
    SELECT [Application ID], 
     [Row Number], 
     [Application Status Movement ID], 
     [Application Status], 
     Comment, 
     [Created Date], 
     [Bond Facilitator], 
     [Status], 

     IIF(COUNT(*) OVER(PARTITION BY [Application ID]) > COUNT([Status]) OVER(PARTITION BY [Application ID], [Status]), 'NO', 'YES') AS [Activity After Audit Completed] 


FROM CTE) AS result 

WHERE [Row Number] IN 
    (SELECT [Row Number] + i 
    FROM CTE 
    CROSS JOIN (SELECT 2 AS i UNION ALL SELECT 3 UNION ALL SELECT 15) AS n 
WHERE [Application Status] = 'Audit Complete') AND [Activity After Audit Completed] IN ('NO') AND Status IN ('FINE', 'NO') 


ORDER BY [Application ID], [Created Date] 

答えて

1

たぶん私は何かを欠場か:ここで

は私がを試してみましたコードは、(あなたは私のコードを短くしたいなら、私は(私はちょうど私が試したものをお見せしたかった)を知ってみましょうですまず、あなたの質問が正しいとは思えませんでしたが、私は解決策がかなりシンプルであると思います。最後のステータスではありません:

DECLARE @t TABLE (Application_ID int, RowNumber int, ApplStatus nvarchar(50)); 

INSERT INTO @t VALUES (926521, 1, 'Work in Progress'), (926521, 2, 'Submitted to Bank'), (926521, 3, 'Audit Complete'), 
         (926521, 4, 'Submitted to Bank'), (926521, 5, 'Audit Complete'); 

INSERT INTO @t VALUES (933633, 1, 'Work in Progress'), (933633, 2, 'Submitted to Bank'), (933633, 3, 'Cancelled'), 
         (933633, 4, 'Submitted to Bank'), (933633, 5, 'Audit Complete'); 

WITH cteRows AS(
    SELECT Application_ID 
     ,ApplStatus 
     ,MIN(RowNumber) OVER (PARTITION BY Application_ID, ApplStatus) rnMinStatus 
     ,MAX(RowNumber) OVER (PARTITION BY Application_ID) rnMax 
    FROM @t 
), 
cteFiltered AS(
    SELECT * 
    FROM cteRows 
    WHERE ApplStatus = 'Audit Complete' 
     AND rnMinStatus < rnMax 
) 
SELECT DISTINCT a.Application_ID, a.ApplStatus 
    FROM @t AS a 
    JOIN cteFiltered AS b ON a.Application_ID = b.Application_ID 
    WHERE a.RowNumber > b.rnMinStatus 
+0

。お役に立てて嬉しいです。あなたが私の答えを受け入れることができれば、あなたのほうがいいでしょう。 – Tyron78

関連する問題