选择Where列的Count()大于1

时间:2016-06-22 15:26:18

标签: mysql count

说我有这样的数据:

ClaimID ClaimLine    Date
   1        1     22/06/2016
   1        1     01/01/2016
   2        1     08/06/2016
   2        2     31/01/2015
   3        1     23/03/2013

如何选择具有最低日期的重复ClaimIDs和ClaimLine?

所以返回值是

1,1,01/01/2016

1 个答案:

答案 0 :(得分:4)

您可以按要删除重复的列进行分组,并从每个组中选择最短日期:

SELECT ClaimID, ClaimLine, MIN(Date)
FROM Table
GROUP BY ClaimID, ClaimLine
ORDER BY ClaimID, ClaimLine

如果您还想要查看已复制的ClaimIDs和ClaimLines,可以添加HAVING子句:

SELECT ClaimID, ClaimLine, MIN(Date)
FROM Table
GROUP BY ClaimID, ClaimLine
HAVING COUNT(*) > 1
ORDER BY ClaimID, ClaimLine