SQL查询不同计数问题

时间:2015-03-06 16:05:32

标签: sql-server count distinct

我正在尝试让每个制造商下的模型数量,在一个表格中我希望显示如下:

Manufacturer       Models
Honda                7
Ford                 12

以模特为例,一致,公民等......

我怎么能算得上这个数字?在SQL表中,它的布局如下

Manufacturer       Models
Honda              Accord
Honda              Civic
Ford               F150
Ford               Taurus

... ETC

在一张桌子上,我想像我上面那样摆出来......

2 个答案:

答案 0 :(得分:2)

使用GROUP BYCOUNT与DISTINCT进行查询:

SELECT  Manufacturer
        ,COUNT(DISTINCT Models) as DistinctModels
FROM    myTable
GROUP BY Manufacturer

答案 1 :(得分:0)

select Manufacturer
     , count(*) total_models
from table
group by Manufacturer
order by Manufacturer

或不同的

select Manufacturer
     , count(distinct Models) total_models
from table
group by Manufacturer
order by Manufacturer