June 24, 2019 at 12:08 am
Hi All,
WITH TempTable (
DuplicateCount,
CustomerID,
ActivityID,
[RelatedReferenceID],
[RelatedReferenceType],
[Due Date],
[TableType],
[Team Member],
[TableStatus]
)
AS
(
SELECT ROW_NUMBER() OVER(PARTITION by CustomerID, ActivityID,[RelatedReferenceType], [Due Date] ORDER BY [Due Date]),
CustomerID,
ActivityID,
[RelatedReferenceID],
[RelatedReferenceType],
[Due Date],
[TableType],
[Team Member],
[tableStatus]
AS DuplicateCount
FROM Tbl_MyTable
)
Select * from TempTable
The problem here is that the correct duplicates are not being returned. Records matching many of these criteria are being returned as duplicate, but they are not. For example, the CustomerID is being grouped where the ActivityID, ReferenceType and DueDate all match.
There has to be a better way. What is it cluey person?
Cheers
June 24, 2019 at 12:12 am
use "distinct" at the bottom of your CTE, right after "select" on the line where you have the "windowing" (over.. partition..)
I am not 100% sure, but, give it a shot
Cheers,
John Esraelo
June 24, 2019 at 12:32 am
Thanks for your quick reply John,
Unfortunately that did not yield a result.
However, I tried something else.
SELECT
T.QTY,
T.CustomerID,
T.ActivityID,
T.RelatedReferenceID,
T.[Due Date],
T.[RecordStarted],
T.[TableSummary],
T.[TableType],
T.[Team Member],
T.[TableStatus]
FROM (
SELECT
S.*
, ROW_NUMBER() OVER (PARTITION by CustomerID, ActivityID, RelatedReferenceID, [Due Date] ORDER BY [Due Date]) AS QTY
FROM Tbl_MyTable S
) T
WHERE T.QTY > 1 and [TableStatus] <>'Closed' AND ActivityID = 58
ORDER BY T.CustomerID, T.ActivityID
This appeared to work, However, I do not know how to delete the second returned record in the query. whereas with my first posting I could just say:
Delete From Tbl_MyTable rather than Select From Tbl_MyTable
I do not want to delete the first instance of the record. only the second instance.
Thanks again
June 28, 2019 at 2:12 pm
Could you add TableStatus to the window function. Perhaps a customer has the same issue as before - It's hard to say exactly without knowing more about the table structure... like is ReferenceID a FK or PK? - I'm probably wrong but figured I'd try to help. Goodluck!
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply