0
SRID    Interaction Flag    Flag2   items1                     items2
16222   abc B   4   1852311000001100    19836811000001100
16222   abc B   4   19836811000001100   1852311000001100
18599   abc B   4   1852311000001100    10368611000001100
18599   abc B   4   10368611000001100   1852311000001100
19518   abc A   2   1592311000001100    1852311000001100
19518   abc A   2   1852311000001100    1592311000001100
19518   abc A   2   1852311000001100    19836811000001100
19518   abc A   2   19836811000001100   1852311000001100

嗨,在我的复杂查询结果集之上。直到这里我能够实现。

请查看前 2 条记录。第 1 行的 item1 = 第 2 行的 item2,SRID 反之亦然。如果 SRID 的 item1 和 item2 在任何顺序上都相同,我需要其中的任何一条记录,结果应该是

 SRID    Interaction    Flag    Flag2   items1                     items2
    16222   abc B   4   1852311000001100    19836811000001100
    18599   abc B   4   1852311000001100    10368611000001100
    19518   abc A   2   1852311000001100    1592311000001100
    19518   abc A   2   1852311000001100    19836811000001100
4

1 回答 1

0

取决于其余数据的情况,但这可能有效....

WITH SourceData AS
(
    SELECT
     *
    FROM (  VALUES   (16222, 'abc', 'B', 4, 1852311000001100, 19836811000001100)
                    ,(16222, 'abc', 'B', 4, 19836811000001100, 1852311000001100)
                    ,(18599, 'abc', 'B', 4, 1852311000001100, 10368611000001100)
                    ,(18599, 'abc', 'B', 4, 10368611000001100, 1852311000001100)
                    ,(19518, 'abc', 'A', 2, 1592311000001100, 1852311000001100)
                    ,(19518, 'abc', 'A', 2, 1852311000001100, 1592311000001100)
                    ,(19518, 'abc', 'A', 2, 1852311000001100, 19836811000001100)
                    ,(19518, 'abc', 'A', 2, 19836811000001100, 1852311000001100)) tab(SRID, Interaction, Flag, Flag2, items1, items2)
)
,CTE AS
(
    SELECT DISTINCT
     s1.*
    FROM SourceData s1
    JOIN SourceData s2
    ON s1.SRID=s2.SRID
    AND s1.items1=s2.items2
)
,CTE2 AS
(
    SELECT
     *
    ,ROW_NUMBER() OVER(PARTITION BY SRID ORDER BY SRID)%2 AS RowID
    FROM CTE
)
SELECT
 SRID
,Interaction
,Flag
,Flag2
,items1
,items2
FROM CTE2
WHERE RowID=1;
于 2013-08-19T13:34:31.413 回答