Oracle SQL:每个错误代码错误计数错误

时间:2015-07-27 12:35:04

标签: sql oracle11g

我正在尝试运行一个SQL查询来查找数据库中每个错误代码的计数。我有两张桌子

  1. sw_sms_events,其中存储了已发送的事务ID和短信。
  2. sw_events其中的事务ID和错误原因如果失败则存储,否则原因始终是"成功发送TariffText"。
  3. 总错误计数: - 从sw_sms_events中选择count(*),其中sms_text喜欢'%Welkom in het buitenland%'

    每个错误原因的总错误数: -

    select distinct count(*) over (partition by b.reason) , b.reason 
    from sw_sms_events a, sw_events b 
    where a.transaction_id= b.transaction_id  
    and a.sms_text like '%Welkom in het buitenland%' 
    and b.reason !='Successfully Sent TariffText'  
    order by (count(*) over (partition by b.reason)) desc
    

    通常这些查询给出相同的结果,即单个错误计数的总和=错误总数。但是在最坏情况下多次重试相同事务的情况下,结果不相同。我们在表中有多行具有相同的事务ID。

    以下是最坏情况下的结果之一:

    Name    24-07-2015
    Total Number of SMSWelcome Sent 156788
    Total Number of Error SMSWelcome    1738
    Total Number of SMSWelcome Sent with null Tariffs   286
    
    
    
    Error Reason    Error Count
    
    Unknown error received :BEA-380000 , ErrorMessage : BSL-99999   1829
    Backend system not available , ErrorMessage : BSL-50002 641
    Remote Error    527
    
    NativeQuery.executeQuery failed , ErrorMessage : BSL-11009  41
    This service is available only for active products , ErrorMessage : BSL-15024   30
    
    Unknown error received :BEA-382556 , ErrorMessage : BSL-99999   18
    
    Customer information: Not retrieved. This action cannot continue without customer information. Please try later or contact your system administrator. , ErrorMessage : BSL-10004    13
    
    OMS login failure: Problem in OMS UAMS login - Nested Exception/Error: java.net.ConnectException: Tried all: '1' addresses, but could not connect over HTTP to server: '195.233.102.177', port: '40123' ,   12
    
    t3://195.233.102.171:30101: Bootstrap to: 195.233.102.171/195.233.102.171:30101' over: 't3' got an error or timed out , ErrorMessage : BSL-11000    5
    getTariffsAndAddOns, status: Failure , ErrorCode : An internal error occured , ErrorMessage : BSL-14005 3
    
    Authorization failed of dealer market restrictions , ErrorMessage : BSL-50005   2
    
    com.amdocs.cih.exception.InvalidUsageException: The input parameter AssignedProductRef is invalid. , ErrorMessage : BSL-10004   1
    

    我的问题是我如何修改当前的sql,使得当我们遇到表中同一事务多次的错误情况时,错误总数应始终等于单个错误计数的总和

2 个答案:

答案 0 :(得分:1)

我真的不明白为什么要使用分析查询。是不是更简单group by

select count(*), b.reason 
from sw_sms_events a, sw_events b 
where a.transaction_id= b.transaction_id  
and a.sms_text like '%Welkom in het buitenland%' 
and b.reason !='Successfully Sent TariffText'  
group by b.reason
order by count(*) desc

当您说我们在表中有多个具有相同事务ID 的行时,您的意思是仅在sw_events表中,还是在sw_sms_eventssw_events表中?

如果是这样,事件会被计算多次,因为您在具有相同transaction_id的所有原始数据上执行笛卡尔积。您应该使用更严格的连接子句。

你也可以做一些事情(非常难看),如:

select count(distinct b.ROWID), b.reason 
from sw_sms_events a, sw_events b 
where a.transaction_id= b.transaction_id  
and a.sms_text like '%Welkom in het buitenland%' 
and b.reason !='Successfully Sent TariffText'  
group by b.reason
order by count(distinct b.ROWID) desc

确保每个事件只计算一次。

答案 1 :(得分:0)

select distinct count(distinct b.ROWID) over (partition by b.reason) , b.reason 
from sw_sms_events a, sw_events b 
where a.transaction_id= b.transaction_id  
and a.sms_text like '%Welkom in het buitenland%' 
and b.reason !='Successfully Sent TariffText'  
order by (count(distinct b.ROWID) over (partition by b.reason)) desc