I try to delete duplicate (service column) but not working
DELETE FROM contactactionnodup
WHERE service IN (SELECT service, COUNT(*), contactid
FROM ContactActionNoDup
GROUP BY service, contactid
HAVING COUNT(*) > 1)
Need to correct this query. Thank you
If you want to keep one of the rows for each service/contactid pair, then use an updatable CTE:
with todelete as (
select ca.*, row_number() over (partition by service, contactid order by service) as seqnum
from ContactActionNoDup as ca
)
delete from todelete
where seqnum > 1;
Related
I have a table where the primary key is a composite key of ID and date. Is there a way that I can delete a single row where ID matches and the date is the latest date?
I am new to SQL, so I have tried a few things, but I either don't get the results I am looking for or cant get the syntax correct
DELETE FROM Master
WHERE ((Identifier = 'SomeID')
AND (EffectiveDate = MAX(EffectiveDate));
There are multiple columns with the same ID, but different dates, ie.
ID EffectiveDate
-------------------------
A '2019-09-18'
A '2019-09-17'
A '2019-09-16'
Is there a way I can delete only the row with A | '2019-09-18'?
You can use window functions and an updatable CTE:
with todelete as (
select t.*, row_number() over (partition by id order by effective_date desc) as seqnum
from t
)
delete from todelete
where seqnum = 1;
Note: If you want to limit this to a single id, then be sure to include a where id = 'a' in either the subquery or outer query.
use row_number()
delete from (select *, row_number() over(partition by id order by effectivedate desc) rn from table_name
) a where a.rn=1
A correlated subquery might get the job done:
DELETE FROM Master
WHERE
Identifier = 'SomeID'
AND EffectiveDate = (
SELECT MAX(EffectiveDate) FROM Master WHERE Identifier = 'SomeID'
)
;
Use the CTE Function to Delete the Row but the below Query will not delete the Record of Max Date of those ID's where Single Record exist against that.
with todelete as (
select t.*, row_number() over (partition by id order by effective_date desc) as seqnum
from t
)
delete from todelete
where seqnum = 1 and id in(select distinct id from todelete where seqnum<>1)
With correlated subquery for all IDs:
delete table1
from table1 t1
where t1.EffectiveDate =
(
select max(t2.EffectiveDate)
from table1 t2
where t2.ID = t1.ID
)
We have a Microsoft SQL Server table [database].[dbo].[UserInAppPurchase] with this columns:
[Id]
,[UserEmail]
,[UserId]
,[PurchaseDate]
,[ProductId]
,[TransactionId]
,[OriginalTransactionId]
,[ValidationTime]
,[ValidationReceipt]
,[ValidFrom]
,[ValidTo]
,[Platfrom]
So a UserID can have multiple records of the same purchase by error. The duplicates would have an identical ValidTo date.
So how would I delete all duplicates? In the end each UserId would have exactly one entry with that particular ValidTo date.
Thanks for the help
Andreas
row_number() with an updatable CTE comes to mind:
with todelete as (
select uiap.*, row_number() over (partition by userid, validto order by id) as seqnum
from UserInAppPurchase uiap
)
delete from todelete
where seqnum > 1;
I am trying to remove some duplicate date from a table called [dbo].[FactGunSales] and the column is [sale_id]. I am checking if there are duplicates with the code below which works and then the code below is the code I am having issues with as it returns no rows affected.
-- Detecting Duplicate
SELECT [sale_id], COUNT(*) TotalCount
FROM [dbo].[FactGunSales]
GROUP BY [sale_id]
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
GO
-- Deleting Duplicate
DELETE FROM [dbo].[FactGunSales]
WHERE [sale_id] NOT IN (SELECT MAX([sale_id])
FROM [dbo].[FactGunSales]
GROUP BY [sale_id])
GO
Any help would be great
Use not exists:
Instead, use ROW_NUMBER() or COUNT(*). Your code seems equivalent to:
WITH todelete AS (
SELECT fgs.*, COUNT(*) OVER (PARTITION BY sale_id) as cnt
FROM [dbo].[FactGunSales] fgs
)
DELETE FROM to_delete
WHERE cnt > 1;
Normally, though, you don't want to delete all duplicates. You want to keep one of them. For that, use ROW_NUMBER():
WITH todelete AS (
SELECT fgs.*, ROW_NUMBER() OVER (PARTITION BY sale_id ORDER BY sale_id) as seqnum
FROM [dbo].[FactGunSales] fgs
)
DELETE FROM to_delete
WHERE seqnum > 1;
Your query doesn't give an indication about which row to keep. This version keeps an arbitrary row. You can keep the newest or oldest or biggest or smallest or whateverest by changing the ORDER BY clause.
Your version doesn't delete anything because at least one value of sale_id is NULL. If any value returned by the subquery is NULL, then the WHERE filters out all rows. Usually, I strongly recommend using NOT EXISTS instead, but for this purpose an updatable CTE makes more sense.
You can consider using a cte and ranking the records on the basis of sale_id, and so any duplicate sale_id would have a rank=2,3,4 etc.. After that you would need to delete entries which are <> rank=1
with cte
as (select row_number() over(partition by sale_id order by sale_id) as rnk
,*
from [dbo].[FactGunSales]
)
delete
from cte
where rnk <> 1
We have a Microsoft SQL Server table [database].[dbo].[UserInAppPurchase] with this columns:
[Id]
,[UserEmail]
,[UserId]
,[PurchaseDate]
,[ProductId]
,[TransactionId]
,[OriginalTransactionId]
,[ValidationTime]
,[ValidationReceipt]
,[ValidFrom]
,[ValidTo]
,[Platfrom]
We have multiple entries with the same [TransactionID], but per TransactionID only one row should be there. Thus we would like to delete all rows with same TransactionID and keep the one with the lowest [Id].
Thanks for the help
Andreas
One nice method uses updatable CTEs:
with todelete as (
select uiap.*,
row_number() over (partition by TransactionID order by id) as seqnum
from UserInAppPurchase uiap
)
delete todelete
where seqnum > 1;
You can, of course, use other methods that are more compatible with other databases, such as:
delete uiap from UserInAppPurchase uiap
where uiap.id > (select min(uiap2.id) from UserInAppPurchase uiap2 where uiap2.TransactionID = uiap.TransactionID);
Here are the columns in my table:
Id
EmployeeId
IncidentRecordedById
DateOfIncident
Comments
TypeId
Description
IsAttenIncident
I would like to delete duplicate rows where EmployeeId, DateOfIncident, TypeId and Description are the same - just to clarify - I do want to keep one of them. I think I should be using the OVER clause with PARTITION, but I am not sure.
Thanks
If you want to keep one row of the duplicate-groups you can use ROW_NUMBER. In this example i keep the row with the lowest Id:
WITH CTE AS
(
SELECT rn = ROW_NUMBER()
OVER(
PARTITION BY employeeid, dateofincident, typeid, description
ORDER BY Id ASC), *
FROM dbo.TableName
)
DELETE FROM cte
WHERE rn > 1
use this query without using CTE....
delete a from
(select id,name,place, ROW_NUMBER() over (partition by id,name,place order by id) row_Count
from dup_table) a
where a.row_Count >1
You can use the following query. This has an assumption that you want to keep the latest row and delete the other duplicates.
DELETE [YourTable]
FROM [YourTable]
LEFT OUTER JOIN (
SELECT MAX(ID) as RowId
FROM [YourTable]
GROUP BY EmployeeId, DateOfIncident, TypeId, Description
) as KeepRows ON
[YourTable].ID = KeepRows.RowId
WHERE
KeepRows.RowId IS NULL