Share via

Delete statement with select and Join

Benjoe 431 Reputation points
2023-06-27T18:05:09.86+00:00

I have a select statement below which gives the expected results. I want to convert this statement to a DELETE Statement but getting error. Below is my query. Any help will be appreciated

SELECT t.*
FROM (
SELECT s.* , COUNT(*) OVER (PARTITION BY s.DocumentName, s.SubmitDateTime, s.DocumentType) AS DocCount
FROM Document s
LEFT OUTER JOIN
Staging b ON s.DocumentId = b.DocumentId
LEFT JOIN Rejections r ON r.DocID = b.DocumentId
WHERE b.DocumentId IS NULL
AND r.DocID IS NULL
AND s.SubmitDateTime IS NOT NULL
AND s.InsertDateTime IS NOT NULL
AND s.DocumentName IS NOT NULL
AND s.Description ='Employee Document'
) t
WHERE t.DocCount > 1
ORDER BY t.DocumentName, t.SubmitDateTime

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


Answer accepted by question author

LiHongMSFT-4306 31,621 Reputation points
2023-06-28T02:31:27.03+00:00

Hi @Benjoe

The DELETE works but I was hoping to and leave one item. The select statement show duplicates. I want to delete and leave one item.

If I understand correctly, it is not appropriate to use COUNT in this query, and the best solution is to use the Row_Number() function.

Try this:

Delete from Document WHERE DocumentId in 
(
SELECT t.DocumentId
FROM (
SELECT s.* , ROW_NUMBER() OVER (PARTITION BY s.DocumentName, s.SubmitDateTime, s.DocumentType) AS [RowCount]
FROM Document s
LEFT OUTER JOIN
Staging b ON s.DocumentId = b.DocumentId
LEFT JOIN Rejections r ON r.DocID = b.DocumentId
WHERE b.DocumentId IS NULL
AND r.DocID IS NULL
AND s.SubmitDateTime IS NOT NULL
AND s.InsertDateTime IS NOT NULL
AND s.DocumentName IS NOT NULL
AND s.Description ='Employee Document'
) t
WHERE t.[RowCount] > 1)

Best regards,

Cosmog Hong


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Jingyang Li 5,901 Reputation points Volunteer Moderator
    2023-06-27T18:25:51.02+00:00

    Delete from Document WHERE DocumentId in (

    SELECT t.DocumentId
    FROM (
    SELECT s.* , COUNT(*) OVER (PARTITION BY s.DocumentName, s.SubmitDateTime, s.DocumentType) AS DocCount
    FROM Document s
    LEFT OUTER JOIN
    Staging b ON s.DocumentId = b.DocumentId
    LEFT JOIN Rejections r ON r.DocID = b.DocumentId
    WHERE b.DocumentId IS NULL
    AND r.DocID IS NULL
    AND s.SubmitDateTime IS NOT NULL
    AND s.InsertDateTime IS NOT NULL
    AND s.DocumentName IS NOT NULL
    AND s.Description ='Employee Document'
    ) t
    WHERE t.DocCount > 1)

    Was this answer helpful?

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.