SQL中的CASE WHEN用法(9)
来源:未知 责任编辑:责任编辑 发表时间:2014-02-02 17:50 点击:次
username action
--------------------------------
A CALL
A SEARCH
A SEARCH
B CALL
B SEARCH
A SEARCH
A VIDEO
B SEARCH
B VIDEO
A CALL
B VIDEO
--------------------------------
通过一条SQL语句得出下列统计数据:
table2
username CALL_TIMES SEARCH_TIMES VIDEO_TIMES
-------------------------------------------------------------------------------
A 2 3 1
B 1 2 2
-------------------------------------------------------------------------------
说明:
table.action 中所有可能的值都已知的,只有 CALL , SEARCH ,VIDEO 和 table2 的字段一一对应
MYSQL 语句:
<BR>select
<BR> t.username,
<BR> max(CASE T.action WHEN 'CALL' THEN T.NN ELSE 0 end) as CALL_TIMES,
<BR> max(CASE T.action WHEN 'SEARCH' THEN T.NN ELSE 0 end) as SEARCH_TIMES,
<BR> max(CASE T.action WHEN 'VIDEO' THEN T.NN ELSE 0 end) as VIDEO_TIMES
<BR>
<BR>from
<BR> (select temp.username,temp.action,count(*) as NN
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>