计算单元格范围内出现的唯一数字的总数

count the total of unique numbers occur in a range of cells

你好,这是我的数据样本

  coustmer_NO  id    
    1            5         
    1            13    
    2            4     
    2            4            
    2            4    
    3            4                
    3            10
    4            8
    4            8

using SQL >> 我想为每个客户计算他们有多少个不同的 ID。 预期输出是:

  coustmer_NO  total_id    
    1            2         
    2            1    
    3            2     
    4            1            

在 MYSQL 中尝试此查询:

select coustmer_NO, count(distinct id) as 'total_id' from table_name group by coustmer_NO;

我猜你的数据有错别字,

结果应该是:

coustmer_NO  total_id    
      1          2         
      2          1    
      3          2     
      4          1            

您可以执行以下操作:
SELECT costumer_NO, count(distinct id) AS total_id FROM <table_name> GROUP BY costumer_NO;