SQL count(*) and distinct

前端 未结 9 1976
野趣味
野趣味 2021-02-12 11:25

Why can\'t we use count(distinct *) in SQL? As in to count all distinct rows?

相关标签:
9条回答
  • 2021-02-12 11:45
    select count (Tag_no) from tab_raw_tag_value where tag_no in (select distinct tag_no from tab_raw_tag_value)
    
    0 讨论(0)
  • 2021-02-12 11:49

    UberKludge, and may be postgre specific, but

    select count( distinct table::text ) from table
    
    0 讨论(0)
  • 2021-02-12 11:50

    why not?

    select 
      count(distinct name)
    from 
      people
    
    0 讨论(0)
  • 2021-02-12 11:50

    some languajes may not be able to handle 'distinct *' so, if you want the distinction made through many columns you might want to use 'distinct ColumnA || ColumnB' , combining the values before judging if they are different. Be mindful whether your variables are numeric and your database handler can make automatic typecast to character strings.

    0 讨论(0)
  • 2021-02-12 12:00

    You can try a CTE in Sql Server 2005

    ;WITH cte AS (
            SELECT  DISTINCT Val1,Val2, Val3
            FROM    @Table
    )
    SELECT  COUNT(1)
    FROM    cte
    

    To answer the question, From the documentation

    Specifies that all rows should be counted to return the total number of rows in a table. COUNT() takes no parameters and cannot be used with DISTINCT. COUNT() does not require an expression parameter because, by definition, it does not use information about any particular column. COUNT(*) returns the number of rows in a specified table without getting rid of duplicates. It counts each row separately. This includes rows that contain null values.

    0 讨论(0)
  • 2021-02-12 12:01

    You can indeed.

    If you've got an identifier, though, you won't have any entirely distinct rows. But you could do for instance:

    SELECT COUNT(DISTINCT SenderID) FROM Messages
    
    0 讨论(0)
提交回复
热议问题