解决count distinct多个字段的方法

Distinct的作用是用于从指定集合中消除重复的元组,经常和count搭档工作,语法如下

COUNT( { [ ALL | DISTINCT ] expression ] | * } )

这时,可能会碰到如下情况,你想统计同时有多列字段重复的数目,你可能会立马想到如下方法:

selectcount( distinct col1 , col2 , col3 , .......) from table

但是,这样是不允许的,因为count是不能统计多个字段的,虽然distinct是可行的。

 

有种比较直接的方法就是把消除重复后在统计查询:

 select count(*) from (select distinct col1 ,col2 , col3 from table)A

posted @ 2020-04-04 11:30  勤奋的园  阅读(5520)  评论(0编辑  收藏  举报