0

我有下一张桌子 -Task(id, type, sessionId, termination, scenario) 我有一个sessionId.

我想选择本次会议常见的所有任务。如果任务具有相同的类型、终止和场景,则它们是相等的。

例子 -

| id | type | sessionId | termination | scenario |
  1     A         20        duration      sc1
  2     B         20       invocation     sc1
  3     C         20        duration      sc2
  4     A         21        duration      sc1
  5     B         21       invocation     sc1

对于sessionId列表等于 (20, 21) - 我想获取下一个信息

| id | type | sessionId | termination | scenario |
  1     A         20        duration      sc1
  2     B         20       invocation     sc1
  4     A         21        duration      sc1
  5     B         21       invocation     sc1 

ids=1,2,4,5 的任务在会话 20 和 21 中很常见。

我设计下一个查询 -

select l.* from Task l 
inner join(select p.* from Task p 
           where 
                p.sessionId in (20,21) 
           group by 
                  p.type, 
                  p.termination, 
                  p.scenario 
           having 
                  count(p.id)=2)s 
on 
   l.type=s.type 
   and l.scenario=s.scenario 
   and l.termination=s.termination;

这是获取此类信息的最佳方式吗?也许有一个更好的查询,它只包含一个select操作并且工作得更快?

4

2 回答 2

0

这是方法。查找与所有会话匹配的所有对类型、终止和场景。然后加入tasks表中以获取所有信息:

select t.*
from tasks t join
     (select type, termination, Scenario
      from tasks
      where sessionId in (20, 21)
      group by type, termination, scenario
      having count(distinct sessionId) = 2
     ) tts
     on tts.type = t.type and tts.termiantion = t.termination and tts.scenario = t.scenario
where t.sessionId in (20, 21);

您需要更改列表和2(如果您更改列表的大小)。

于 2013-04-01T20:38:55.947 回答
0

My below code does not limit to any particular sub-set of sessionID.

SELECT t1.* 
FROM Task AS t1 INNER JOIN Task AS t2 ON t1.type = t2.type AND t1.termination = t2.termination AND
                                   t1.scenario = t2.scenario AND t1.sessionid <> t2.sessionid AND
                                   t1.id <> t2.id
ORDER BY t1.id 

Here's SQL Fiddle: http://sqlfiddle.com/#!2/55d7c/9 Feel free to experiment.

Have I somewhat misunderstood your requirements?

于 2013-04-01T21:02:21.533 回答