根据HistoryID删除SQL重复字段
我有一个包含重复值的数据库。具体来说,SiteCode、LastName、FirstName、DateofService、Payer、BilledAmount、NetReceivable 和 ContractualDiscount 连接形成在此表中重复出现的记录。
我想删除这些字段中除一个实例以外的所有字段,并尝试通过仅选择一个 NetBilledHistoryID(这是表中每条记录的唯一字段)来实现此目的。
不幸的是,当我运行此查询时,我仍然得到重复的值。
我该如何纠正这个问题,以便我的选择查询消除这些重复项?或者,更好的是,我应该一起使用不同的查询技术吗?
SELECT *
FROM [Reports].[dbo].[NetBilledHistory] t1
WHERE EXISTS (
SELECT 1 FROM [Reports].[dbo].[NetBilledHistory] AS t2
WHERE t2.SiteCode = t1.SiteCode
AND t2.LastName = t1.LastName
AND t2.FirstName = t1.FirstName
AND t2.DateofService = t1.DateofService
AND t2.Payer = t1.Payer
AND t2.BilledAmount = t1.BilledAmount
AND t2.NetReceivable = t1.NetReceivable
AND t2.ContractualDiscount = t1.ContractualDiscount
AND t2.NetBilledHistoryID < t1.NetBilledHistoryID)
I have a database with duplicate values. Specifically SiteCode, LastName, FirstName, DateofService, Payer, BilledAmount, NetReceivable, and ContractualDiscount concatenate to form records that recur repeatedly throughout this table.
I'd like to remove all but one instance of these fields and am trying to do so by picking just one NetBilledHistoryID (which is a unique field for each record in the table).
Unfortunately, when I run this query, I still get the duplicated values.
How can I correct this so that my select query eliminates these duplicates? Or, even better, should I be using a different query technique all together?
SELECT *
FROM [Reports].[dbo].[NetBilledHistory] t1
WHERE EXISTS (
SELECT 1 FROM [Reports].[dbo].[NetBilledHistory] AS t2
WHERE t2.SiteCode = t1.SiteCode
AND t2.LastName = t1.LastName
AND t2.FirstName = t1.FirstName
AND t2.DateofService = t1.DateofService
AND t2.Payer = t1.Payer
AND t2.BilledAmount = t1.BilledAmount
AND t2.NetReceivable = t1.NetReceivable
AND t2.ContractualDiscount = t1.ContractualDiscount
AND t2.NetBilledHistoryID < t1.NetBilledHistoryID)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过使用
CTE
来实现这一点:根据需要进行调整以适合您的列等用途。
you can achieve that by using
CTE
:adjust as needed to fit your purpose for columns, etc.