SELECT COUNT(DISTINCT… ) error on multiple columns?

后端 未结 3 2175
囚心锁ツ
囚心锁ツ 2021-02-12 19:18

I have a table, VehicleModelYear, containing columns id, year, make, and model.

The following two queries work as expected:

SELECT DISTINCT make, model
F         


        
3条回答
  •  甜味超标
    2021-02-12 19:45

    COUNT() in SQL Server accepts the following syntax

    COUNT(*)
    COUNT(colName)
    COUNT(DISTINCT colName)
    

    You can have a subquery which returns unique set of make and model that you can count with.

    SELECT  COUNT(*)
    FROM
            (
                SELECT  DISTINCT make, model
                FROM    VehicleModelYear
            ) a
    

    The "a" at the end is not a typo. It's an alias without which SQL will give an error ERROR 1248 (42000): Every derived table must have its own alias.

提交回复
热议问题