MYSQL 如何對一個欄位進行分類統計

來源:互聯網
上載者:User

例如:如所示,對資料庫的EmailSource欄位按日期分類匯總

因此我們可以寫入如下的SQL語句類解決這樣的問題

SELECT sum(case when `EmailSource`='FM' then 1 else 0 end) as FM_Statistic,sum(case when `EmailSource`='UOC' then 1 else 0 end) as UOC_Statistic,sum(case when `EmailSource`='OC' then 1 else 0 end) as OC_Statistic,DATE_FORMAT(Date,'%Y-%m-%d') AS `DateTime` FROM `user_performance` WHERE Email != '' AND Email != 'TOTAL'AND (DATE_FORMAT(Date,'%Y-%m-%d') >= DATE_FORMAT('2011-02-5','%Y-%m-%d')) AND (DATE_FORMAT(Date,'%Y-%m-%d') <= DATE_FORMAT('2011-03-07','%Y-%m-%d')) GROUP BY `Date`

搜尋後結果為:

FM_Statistic UOC_Statistic OC_Statistic DateTime
1 0 1 2011-02-07
2 0 2 2011-02-08
26 0 26 2011-02-09
26 0 26 2011-02-10
28 0 28 2011-02-11
31 0 31 2011-02-14
31 0 31 2011-02-15
30 0 30 2011-02-16
29 0 29 2011-02-17
28 0 28 2011-02-18
31 0 31 2011-02-21
32 0 32 2011-02-22
30 0 30 2011-02-23
32 0 32 2011-02-24
31 31 31 2011-02-25
4 4 4 2011-02-26
5 5 5 2011-02-27
29 29 29 2011-02-28
32 32 32 2011-03-01
30 30 30 2011-03-02
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.