2015-10-25 55 views
1

計數與條件組的所有項目我都喜歡下面按日期

| 2015-10-01 00:18:55 | 100021397 | canceled   | 
| 2015-10-01 02:05:16 | 100021398 | processing   | 
| 2015-10-01 02:15:23 | 100021399 | processing   | 
| 2015-10-01 02:17:40 | 100021400 | processing   | 
| 2015-10-01 02:28:02 | 100021401 | processing   | 
| 2015-10-01 02:31:39 | 100021402 | processing   | 
| 2015-10-01 02:44:13 | 100021403 | canceled   | 
| 2015-10-02 00:00:35 | 100021519 | canceled   | 
| 2015-10-02 01:05:22 | 100021520 | processing   | 
| 2015-10-02 01:06:42 | 100021521 | processing   | 

我需要的輸出組按日期格式

Date   | Number Of all Item | Number of Item with status not canceled | 
2015-10-01 |  7    |   5 
2015-10-02 |  3    |   2 

數據創建第一個兩米欄是很容易的,然而,第三列有點棘手,請幫助

回答

1

SUM()你可以「計數」布爾表達式(返回0或1,false或true)的結果。

SELECT DATE(date), COUNT(DISTINCT item), SUM(status != 'canceled') 
FROM your_table 
GROUP BY DATE(date); 
1
select 
    date(`date`) as `date`, 
    count(*) as `Number Of all Item`, 
    sum(if(status!='canceled',1,0)) as `Number of Item with status not canceled` 
from tbl1 group by date(`date`)