简体   繁体   中英

SQL deleting records with group by multiple tables

I am trying to delete duplicate records in a table but on if they are duplicate per a record from another.

The following query gets me the number of duplicate records per 'bodyshop'. Im trying to delete multiple invoices for each bodyshop.

SELECT
    inv.InvoiceNo, job.BodyshopId, COUNT(*)
FROM
    [Test].[dbo].[Invoices] as inv
  join [Test].[dbo].Repairs as rep on rep.Id = inv.RepairId
  join [Test].[dbo].Jobs as job on job.Id = rep.JobsId
GROUP BY
    inv.InvoiceNo, job.BodyshopId
HAVING 
    COUNT(*) > 1

I want the duplicate invoice numbers per bodyshop to be deleted but i do want the original one to remain.

InvoiceNo   BodyshopId  (No column name)
29737        16          2
29987        16          3
30059        16          2
23491        139         2
23608        139         3
23867        139         4
23952        139         3

I only want invoice number 29737 to be once against bodyshopid 16 etc.

Hope that makes sense

Thanks

You may run the following as two records are same so, Group by will return single row for same invoice:

DELETE FROM inv where id not in (
SELECT Max(inv.id) FROM (
SELECT
    inv.id, inv.InvoiceNo, job.BodyshopId, COUNT(*)
FROM
    [Test].[dbo].[Invoices] as inv
  join [Test].[dbo].Repairs as rep on rep.Id = inv.RepairId
  join [Test].[dbo].Jobs as job on job.Id = rep.JobsId
GROUP BY
    inv.InvoiceNo, job.BodyshopId
HAVING 
    COUNT(*) > 1
) TMP_TABLE )

id is the primary key.

General SQL. Modify if needed for sql-server.

Perhaps this :

with cte as (
SELECT
    inv.ID, inv.InvoiceNo, job.BodyshopId, rn = row_number() over (partition by inv.InvoiceNo, job.BodyshopId order by inv.InvoiceNo, job.BodyshopId)
FROM
    [Test].[dbo].[Invoices] as inv
  join [Test].[dbo].Repairs as rep on rep.Id = inv.RepairId
  join [Test].[dbo].Jobs as job on job.Id = rep.JobsId
)

delete t1
from [Test].[dbo].[Invoices] t1 inner join cte t2 on t1.ID = t2.ID 
where t2.rn > 1

Edit 1 - Your comments are trues. So a solution is to add an identity column to the invoice table. I've adapt my query.

To add / remove an identity column :

 alter table [Test].[dbo].[Invoices]  id int identity(1,1)
 drop  column id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM