我正在尝试删除表中的重复记录,但是如果它们是来自另一个记录的重复记录,则会显示。
以下查询获取每个'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
我希望删除每个bodyshop的重复发票号码,但我确实希望保留原始号码。
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
我只希望发票编号29737一次反对bodyshopid 16等。
希望有意义
由于
答案 0 :(得分:0)
您可以运行以下内容,因为两条记录相同,因此Group by
将返回同一发票的单行:
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是主键。
常规SQL。如果需要修改sql-server。
答案 1 :(得分:0)
也许这就是:
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
编辑1 - 您的评论是真实的。因此,解决方案是将标识列添加到发票表中。我调整了我的查询。
添加/删除标识列:
alter table [Test].[dbo].[Invoices] id int identity(1,1)
drop column id