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

SQL Anywhere:查找与另一行相比为-2的行

  •  0
  • Heather  · 技术社区  · 7 年前

    我有下表:

    ID  User  Form  Depth
    1   A     ABC   2001
    1   A     XYZ   1001
    1   B     XYZ   1003
    1   B     DEF   3001
    1   C     XYZ   1000
    

    如果ID和Form是相同的,我需要从用户A中标识那些+2的行。使用上面的示例,脚本将返回:

    ID  User  Form  Depth
    1   B     XYZ   1003
    1   C     XYZ   1000
    

    有人有什么建议吗?

    谢谢!

    3 回复  |  直到 7 年前
        1
  •  1
  •   markp-fuso    7 年前

    如果你要找的深度正好是A深度的正负2:

    select t1.*
    from   mytab t1,
           mytab t2
    where  t1.id    = t2.id
    and    t1.form  = t2.form
    and    t1.user != 'A'
    and    t2.user  = 'A'
    and    abs(t1.depth - t2.depth) = 2
    go
    
    ID  User  Form  Depth
    --- ----- ----- -----
    1   B     XYZ   1003
    

    如果您希望深度在A深度的2以内(即diff<=2):

    select t1.*
    from   mytab t1,
           mytab t2
    where  t1.id    = t2.id
    and    t1.form  = t2.form
    and    t1.user != 'A'
    and    t2.user  = 'A'
    and    abs(t1.depth - t2.depth) <= 2
    go
    
    ID  User  Form  Depth
    --- ----- ----- -----
    1   B     XYZ   1003
    1   C     XYZ   1000
    

    sql fiddle

        2
  •  0
  •   Gordon Linoff    7 年前

    我想你想要 exists :

    select t.*
    from t
    where t.user <> 'A' and
          exists (select 1
                  from t t2
                  where t2.form = t.form and t2.id = t.id and
                        t2.depth between t.depth - 2 and t.depth + 2
                 );
    
        3
  •  0
  •   Edward    7 年前

    替换 @User 不管你想带走谁。

    DECLARE @table TABLE ( 
        ID Int
        ,[User] VARCHAR(2) 
        ,Form VARCHAR(3)
        ,Depth INT 
    ) 
    
    DECLARE @User VARCHAR(2) = 'A' 
    
    INSERT INTO @table (ID , [User], Form, Depth)
    VALUES 
        (1 , 'A' , 'ABC' , 2001),
        (1 , 'A' , 'XYZ' , 1001),
        (1 , 'B' , 'XYZ' , 1003),
        (1 , 'B' , 'DEF' , 3001),
        (1 , 'C' , 'XYZ' , 1000)
    
    SELECT t1.ID, t1.[User], t1.Form, t1.Depth , ROW_NUMBER() OVER(ORDER BY t1.ID, t1.[User], t1.Form, t1.Depth) AS [row_number] 
    INTO #temp 
    FROM @table as t1 
    INNER JOIN ( 
        SELECT t.ID, t.Form, COUNT('8') as [count] 
        FROM @table as t
        GROUP BY ID, Form 
        HAVING COUNT('8') > 1 
    ) as duplicates
    ON duplicates.ID = t1.ID 
    AND duplicates. Form = t1.Form 
    ORDER BY ID, User, Form, Depth
    
    
    -- SELECT * FROM #temp 
    
    SELECT [row_number] - 2 as value 
    INTO #range 
    FROM #temp as t
    WHERE t.[User] = @User
    
    
    --SELECT * FROM #range
    
    INSERT INTO #range 
    SELECT [row_number] - 1 
    FROM #temp as t 
    WHERE t.[User] = @User
    
    INSERT INTO #range 
    SELECT [row_number] + 1 
    FROM #temp as t
    WHERE t.[User] = @User
    
    INSERT INTO #range 
    SELECT [row_number] + 2
    FROM #temp as t 
    WHERE t.[User] = @User
    
    SELECT * FROM #temp 
    WHERE [row_number] IN (SELECT value FROM #range)
    
    
    DROP TABLE #temp 
    DROP TABLE #range