Finding duplicate values

感情迁移 提交于 2019-12-04 05:28:54

问题


I have a table called Transfers, and I would like to find all the records that have duplicate values on three columns Doc ID,Amount and Date. Basically what I need is to find where Doc id ,amount and dates are the same What is the best query I can use to find these duplicates?

I tried the following query

select transfers.doc_id,transfers.date,transfers.amount,
  from transfers 
 where transfers.date between $P{StartDate} and $P{EndDate}
 group by doc_id
having doc_id >1;

Here is what results am looking for:

Doc_id  Date        amount
1234    12/07/2019  3,000
1234    12/07/2019  3,000
2345    12/07/2019  15,000
2345    12/07/2019  15,000
4321    12/07/2019  5,600
4321    12/07/2019  5,600

回答1:


Use This:

select transfers.doc_id,transfers.date,transfers.amount,count(*) counts
from transfers
group by doc_id,date,amount
having counts>1;

Output will be with Count of duplicate Records:

doc_id  date        amount  counts
1234    12/07/2019  3,000   2
2345    12/07/2019  15,000  2
4321    12/07/2019  5,600   2



回答2:


Consider using group by for three columnsgroup by doc_id,Amount,Date

select t.doc_id,t.date,t.amount
  from transfers t
 where t.date between $P{StartDate} and $P{EndDate}
 group by doc_id,Amount,Date
having count(doc_id) >1;


来源:https://stackoverflow.com/questions/57252233/finding-duplicate-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!