日期:2014-05-16  浏览次数:21167 次

关于distinct的问题
有张表记录了用户的点击事件,现在我要计算多少用户点击了(重复的不算),有两个字段,一个是subscriber_id一个是list_id,只有subscriber_id和list_id都一样才能代表同一个人,请问要如何统计呢?

------解决方案--------------------
给出更详细的表结构
------解决方案--------------------
select count(distinct list_id) from tablename where list_id=subscriber_idgroup by subscriber_id
------解决方案--------------------
我给的是错的
------解决方案--------------------
select count(*) from tablename group by subscriber_id,list_id order by null
------解决方案--------------------
select count(*) as iddd from table_name where list_id=subscriber_id group by subscriber_id
------解决方案--------------------
select count(1) from table1 where list_id = subscriber_id group by list_id,subscriber_id;
------解决方案--------------------
select count(*) from (select * from tablename group by subscriber_id,list_id order by null)
------解决方案--------------------
如果要排除subscriber_id或list_id的NULL值用下面的语句:
select count(*) from (select count(*) as num from tablename where subscriber_id is not null and list_id is not null group by subscriber_id,list_id) b

如果要subscriber_id和list_id都没有NULL值可以用下面的语句:
select count(*) from (select count(*) as num from tablename group by ubscriber_id,list_id) b
------解决方案--------------------
select sum(cnt) from (select count(*) as cnt form tablename group by subscriber_id,list_id) tmp