简体   繁体   English

oracle 10g中的子查询

[英]subquery in oracle 10g

I have a table in oracle 10g database with following columns 我在oracle 10g数据库中有一个表,其中包含以下各列

sdate                  sid    event  
31/10/2013 20:20:20     1        A    
31/10/2013 20:20:21     1        B   
31/10/2013 20:20:22     1        C   
31/10/2013 20:41:04     2        A    
31/10/2013 20:41:10     2        B   
31/10/2013 20:42:20     2        C    
31/10/2013 20:42:49     2        C   
31/10/2013 21:20:50     3        A   
31/10/2013 21:21:33     3        B   
31/10/2013 21:23:00     3        C   
31/10/2013 21:08:20     4        B    
31/10/2013 21:11:20     4        C  
31/10/2013 21:17:20     4        C 
31/10/2013 22:20:20     5        C 
31/10/2013 22:22:22     5        D  

sdate is the date of this record, sid is like a session ID, event is unique event within a session eg A, B , C etc. A session should always start with one occurrence of event 'A'. sdate是此记录的日期, sid类似于会话ID,事件是会话中的唯一事件,例如A,B,C等。会话应始终以事件“ A”的出现开始。 Other events like B, C can repeat. 其他事件,如B,C可以重复。

Can some one please help me to write a query that will give me a list of all SID, which start with an event other than 'A' for example sid 4 and 5 because tese sessions start with B and C. 有人可以帮我写一个查询,为我提供所有SID的列表,该列表以'A'以外的事件开头,例如sid 4和5,因为这些会话以B和C开头。

UPDATE: I have abbreviated events to A,B,C the actual events in my table a long strings. 更新:我已将事件缩写为A,B,C,我表中的实际事件是一个长字符串。

You can use analytic functions for this purpose: 您可以为此使用分析功能:

select t.*
from (select t.*, row_number() over (partition by sid order by sdate) as seqnum
      from table t
     ) t
where seqnum = 1 and event <> 'A';

row_number() enumerates the events for each sid based on the date . row_number()根据date枚举每个sid的事件。 The where clause chooses the first one and all such rows where the event is not an 'A' . where子句选择第一个以及事件不是'A'所有此类行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM