Search code examples
sassas-macro

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


Hello this is my data sample

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

using SQL >> I Would like to count for each customer how many different ID They have. the expected output is:

  coustmer_NO  total_id    
    1            2         
    2            1    
    3            2     
    4            1            

Solution

  • I guess there is a typo in your data,

    The result should be:

    coustmer_NO  total_id    
          1          2         
          2          1    
          3          2     
          4          1            
    

    You can do the following:
    SELECT costumer_NO, count(distinct id) AS total_id FROM <table_name> GROUP BY costumer_NO;