查找具有不同最后一列的所有重复项的计数

Finding a count of all duplicates with a different last colum

我有 table 份保单,我正在尝试查找具有完全相同信息的所有保单,但邮政编码不同(最后一列)。

如果有人对如何找到它有任何建议,我们将不胜感激

Policy    | Expiry   |  State|     Region  |  InsuredValue  |   Construction |  BusinessType| Zip

100242    | 2-Jan-21 |   NY |       East   |    1,617,630   |     Frame      |  Retail      | 10002
100242    | 2-Jan-21 |   NY |       East   |    1,617,630   |     Frame      |  Retail      | 10011
100242    | 2-Jan-21 |   NY |       East   |    1,617,630   |     Frame      |  Retail      | 10005
100242    | 2-Jan-21 |   NY |       East   |    1,617,630   |     Frame      |  Retail      | 10002
100242    | 2-Jan-21 |   NY |       East   |    1,617,630   |     Frame      |  Retail      | 10005
100242    | 2-Jan-21 |   NY |       East   |    1,617,630   |     Frame      |  Retail      | 10005
100242    | 2-Jan-21 |   NY |       East   |    1,617,630   |     Frame      |  Retail      | 10011
100242    | 2-Jan-21 |   NY |       East   |    1,617,630   |     Frame      |  Retail      | 10011
100242    | 2-Jan-21 |   NY |       East   |    1,617,630   |     Frame      |  Retail      | 10005
100242    | 2-Jan-21 |   NY |       East   |    1,617,630   |     Frame      |  Retail      | 10011
100242    | 2-Jan-21 |   NY |       East   |    1,617,630   |     Frame      |  Retail      | 10005
100242    | 2-Jan-21 |   NY |       East   |    1,617,630   |     Frame      |  Retail      | 10002
100242    | 2-Jan-21 |   NY |       East   |    1,617,630   |     Frame      |  Retail      | 10005

在这个例子中我希望它是 return:

Policy       Expiry     State    Region       InsuredValue    Construction    BusinessType   dupCount 

100242    | 2-Jan-21 |   NY |       East   |    1,617,630   |     Frame      |  Retail      | 3

dupCount 为 3,因为此数据有 3 个不同的邮政编码

只需按所有列(Zip 除外)使用分组并按 Zip 计数。

SELECT policy,
       expiry,
       state,
       region,
       insuredvalue,
       construction,
       businesstype,
       COUNT(distinct zip) AS dupcount
    FROM my_table
    GROUP BY policy,
             expiry,
             state,
             region,
             insuredvalue,
             construction,
             businesstype
    HAVING COUNT(distinct zip) > 1;

您可以试试下面的方法。

select t.Policy, t.Expiry, t.State, t.Region, t.InsuredValue, t.Construction, t.BusinessType, count(1) 
from (
  select distinct Policy, 
  Expiry, State, Region, 
  InsuredValue, Construction, 
  BusinessType, Zip from tablename) t
group by t.Policy, t.Expiry, t.State, t.Region, t.InsuredValue, t.Construction, t.BusinessType;