Count distinct value pairs in multiple columns in SQL

前端 未结 5 1969
终归单人心
终归单人心 2021-01-31 17:14

What query will count the number of rows, but distinct by three parameters?

Example:

Id      Name        Address 
==============================
1     My         


        
相关标签:
5条回答
  • 2021-01-31 17:24

    Another (probably not production-ready or recommended) method I just came up with is to concat the values to a string and count this string distinctively:

    SELECT count(DISTINCT concat(id, name, address)) FROM mytable;
    
    0 讨论(0)
  • 2021-01-31 17:26

    Get all distinct id, name and address columns and count the resulting rows.

    SELECT COUNT(*) FROM mytable GROUP BY id, name, address
    
    0 讨论(0)
  • 2021-01-31 17:26

    You can also do something like:

    SELECT COUNT(DISTINCT id + name + address) FROM mytable
    
    0 讨论(0)
  • 2021-01-31 17:33

    Having to return the count of a unique Bill of Materials (BOM) where each BOM have multiple positions, I dd something like this:

    select t_item, t_pono, count(distinct ltrim(rtrim(t_item)) + cast(t_pono as varchar(3))) as [BOM Pono Count]
    from BOMMaster
    where t_pono = 1
    group by t_item, t_pono
    

    Given t_pono is a smallint datatype and t_item is a varchar(16) datatype

    0 讨论(0)
  • 2021-01-31 17:40

    To get a count of the number of unique combinations of id, name and address:

    SELECT Count(*)
    FROM   (
            SELECT DISTINCT
                   id
                 , name
                 , address
            FROM   your_table
           ) As distinctified
    
    0 讨论(0)
提交回复
热议问题