私は可能な限りこれを置くでしょう。特定のステータスの後に同じIDの行を返す
私は、同じApplicationIDを持つ異なるApplicationStatusを持つ列を持っています。 ApplicationStatusが 'Audit Complete'に初めて到達したときにそれ以降にアクティビティが発生した場合、それらのApplicationStatusを返す必要があります( 'Audit Complete' ApplicationStatusの後に 'Audit Complete'が再度表示されても)。それはまた、それらの行を返すべきではありませんAPPLICATIONID年代のグループに表示され
これは私が(他の列には何の問題もありません)を返したいものです:。
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]
。お役に立てて嬉しいです。あなたが私の答えを受け入れることができれば、あなたのほうがいいでしょう。 – Tyron78