You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写Select Query查询表中重复值并返回带计数的指定格式结果

Query to Find Duplicate Records with Count

Alright, based on your table structure and sample data, it looks like we need to identify duplicates where CustomerName, DocumentValue, and Date all match (since those are the fields that repeat across your target rows). Here's a SQL query that will return each duplicate record along with the total number of times that combination repeats:

SELECT 
    Docnum,
    CustomerName,
    DocumentValue,
    Date,
    COUNT(*) OVER (PARTITION BY CustomerName, DocumentValue, Date) AS Count
FROM 
    YourTableName
WHERE 
    COUNT(*) OVER (PARTITION BY CustomerName, DocumentValue, Date) > 1
ORDER BY 
    CustomerName, Date, Docnum;

How this works:

  • PARTITION BY CustomerName, DocumentValue, Date: This groups records by the three fields that define a duplicate. The COUNT(*) will calculate how many records are in each group.
  • The WHERE clause filters out any groups that only have one record (so we only keep duplicates).
  • The ORDER BY is optional but helps organize the results to show matching duplicates together.

Sample Output:

DocnumCustomerNameDocumentValueDateCount
101ABC1014-04-182
102ABC1014-04-182
105KFB1216-04-183
106KFB1216-04-183
107KFB1217-04-183

Just replace YourTableName with the actual name of your table, and this should work for most modern SQL databases (like PostgreSQL, SQL Server, MySQL 8.0+, etc.).

内容的提问来源于stack exchange,提问作者Naveen.A

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:07:00