根据以下值计算

时间:2017-04-28 04:01:26

标签: oracle

是否可以根据上述值和条件获得计数。我正在使用Oracle 11G

见下表。

我想要计算Mr.X,其中交易类型=电子邮件,上面的Byuser是系统。

CallID | Transactiontype | Byuser

1234     | Email            | system
1235     | Email            | MR.X
1236     | call             | Mr.X
1237     | Email            | System
1238     | Email            | Mr.X
1239     | Call             | Mr.Y
1240     | Other            | Mr.X

最终结果将是:

Byuser |计数

先生。 X | 2

3 个答案:

答案 0 :(得分:0)

试试这个(已编辑):

 select t.Byuser, count(*)
 from table_name t join table_name f
 on t.callid=f.callid+1
 where t.Transactiontype ='Email' and t.Byuser='MR.X' and f.Byuser='System'
 group by t.Byuser ;

答案 1 :(得分:0)

在这种情况下,您可以使用分析函数lag来查找最后一行中的ByUser值,并在当前行中进行比较:

select ByUser,
    count(*)
from (
    select t.*,
        lag(ByUser) over (
            order by callId
            ) as last_ByUser
    from your_table t
    where transactionType = 'Email'
    ) t
where lower(ByUser) = 'mr.x'
    and lower(last_ByUser) = 'system'
group by ByUser;

答案 2 :(得分:0)

如果记录的预期顺序由ID驱动,则以下查询将起作用。

SELECT 
      NAME,
      COUNT(NAME)
FROM
      (SELECT NAME,
      LAG(NAME, 1) OVER (ORDER BY ID) AS PNAME
      FROM MYTABLE 
      WHERE TRANSACTION_TYPE = 'EMAIL'
      ORDER BY ID
      )
WHERE PNAME = 'SYSTEM' AND NAME = 'MR.X'
GROUP BY NAME