mysql:如何获取具有多个groupby的唯一记录的计数

时间:2014-11-08 13:57:30

标签: php mysql

我需要像以下结构一样的唯一记录的计数

DATE      | STATUS |  EMPLOYEE|UNIQUEDATE|UNIQUESTATUS|UNIQUEEMPLOYEE
20-1-2013   active    John          7         3             2
20-1-2013   active    John          7         3             2
20-1-2013   active    Kyle          7         3             1
20-1-2013   inactive  mohan         7         4             1
20-1-2013   inactive  subbu         7         4             3
20-1-2013   inactive  subbu         7         4             3
20-1-2013   inactive  subbu         7         4             3

计数基于生成的输出值

我尝试了这个查询:

select DATE,STATUS,EMPLOYEE,COUNT(DATE) as DATECOUNT,COUNT(STATUS ) as DATESTATUS ,COUNT(EMPLOYEE) as DATEEMPLOYEE from dailyincome group by DATE,STATUS,EMPLOYEE

但所有三列的计数都相同。

1 个答案:

答案 0 :(得分:-1)

试试这个:

select DATE,STATUS,EMPLOYEE,
  COUNT( DISTINCT DATE) as DATECOUNT,
  COUNT( DISTINCT STATUS ) as DATESTATUS ,
  COUNT( DISTINCT EMPLOYEE) as DATEEMPLOYEE 
from dailyincome group by DATE,STATUS,EMPLOYEE

我无法访问您的数据,因此无法测试,但是" distinct" COUNT()函数中的关键字应该可以满足你的需要。我们可能需要添加GROUP BY子句