【发布时间】:2015-07-07 06:24:58
【问题描述】:
我一直在尝试从行中获取一些结果以呈现到列中。
桌子就像,
Event_ID
User ID
Event_Type (Call,App)
...
一个用户可以有多个事件类型,呼叫或约会。我想将结果列为
User_ID Calls_Count Appointments_Count
01 4 10
02 0 12
我已设法编写查询以获得所需的结果,但如果用户没有呼叫或约会,则记录不会出现在结果中。
select
distinct(e.user_id),
Call.cc as Call_Count,
App.ac as App_Count
from events e,
(select user_id,event_type,count(user_id) as cc from events c where event_type = 'Call' group by user_id,event_type) Call,
(select user_id,event_type,count(user_id) as ac from events a where event_type = 'App' group by user_id,event_type) App
where e.user_id = Call.user_id
and e.user_id = App.user_id
order by user_id asc
;
如何将此查询转换为使用join,以便它返回所需的结果,或者有更好的方法来实现相同的结果。
在此先感谢
【问题讨论】: