0

我在尝试每月返回一些事件时遇到了麻烦。

我有一张桌子,有 3 列;回拨、跟进、服务。我要做的是返回每个帐户每月发生的每个事件的次数。

我试图使用子查询:

Select...<columns>...
(
    select 
    COUNT(eventname)
    from 
    tworkorderevent 
    group by eventname
    having 
    eventname = 'Follow up'
)FollowUps,
from <tablename>
where...
group by ...

但这只是返回事件的总数,而不是每个帐户每月的数量。

任何关于澄清的建议或要求都将受到赞赏和接受。谢谢你。

4

1 回答 1

0

You should be able to do this then:

Select a.account_id, b.follow_up_count, c.callback_count, d.service_count
From tworkorderevent a
Left Outer Join
(
    select account_id, COUNT(eventname) as follow_up_count
    from tworkorderevent
    where eventname like 'Follow up'
    group by account_id
)b On a.account_id = b.account_id
Left Outer Join
(
    select account_id, COUNT(eventname) as callback_count
    from tworkorderevent
    where eventname like 'Callback'
    group by account_id
)c On a.account_id = c.account_id
Left Outer Join
(
    select account_id, COUNT(eventname) as service_count
    from tworkorderevent
    where eventname like 'Service'
    group by account_id
)d On a.account_id = d.account_id

Just replace the "account_id" references with whatever your primary key is, and ensure that the values for each "Where" statement are correct.

于 2013-06-04T17:08:10.627 回答