代码之家  ›  专栏  ›  技术社区  ›  Sree

一个表中成对术语的SQL查询

sql
  •  1
  • Sree  · 技术社区  · 14 年前

    Example:S1  T1  T2
            S1  T1  T3
            S1  T2  T3
    

    这是正确的输出形式,但是一旦T1,T2已经在输出表中配对,就不应该得到S1 T2 T1和S1 T3 T2。

    给定表:

       SEQID  TID                    
    
       S1      T1                 
       S1      T2                      
       S1      T3                      
       S2      T2                      
       S2      T3                       
       S2      T5                     
       S2      T6
    

     SEQID             TID       TID                
       S1              T1         T2
       S1              T1         T3               
       S1              T2         T3                
       S2              T2         T3               
       S2              T2         T5
       S2              T2         T6
       S2              T3         T5
       S2              T3         T6
       S2              T5         T6
    

    提前谢谢。。

    2 回复  |  直到 14 年前
        1
  •  0
  •   Jeremy Wiggins    14 年前

    请尝试以下操作:

    
    SELECT      a.seqid, a.tid, b.tid
    FROM        [TABLE] a
    INNER JOIN  [TABLE] b on a.seqid = b.seqid
    WHERE       a.tid < b.tid
    ORDER BY    a.seqid, a.tid
    
    
        2
  •  3
  •   Quassnoi    14 年前
    SELECT  gt1.seqid, gt1.tid, gt2.tid
    FROM    giventable gt1
    JOIN    giventable gt2
    ON      gt2.seqid = gt1.seqid
            AND gt2.tid > gt1.tid