SQL Server 2005:如何只连接一次表行

时间:2014-07-15 17:24:58

标签: sql sql-server sql-server-2005

我认为我已经看到了针对MySQL的类似问题的答案,但我很难找到适用于SQL Server 2005的答案。

所以我有一张这样的表:

| ID | RelationalID | Year
----------------------------
| 1  | A            | 2014
| 2  | A            | 2014
| 3  | B            | 2014
| 4  | A            | 2015
| 5  | B            | 2015

当我加入RelationID匹配的同一个表但年份不同时,我喜欢这样的结果:

| 2014_ID | 2015_ID | RelationalID |
------------------------------------
| 1       | 4       | A            |
| 2       | NULL    | A            |
| 3       | 5       | B            |

但标准的JOIN最终会获得重复的匹配:

| 2014_ID | 2015_ID | RelationalID |
------------------------------------
| 1       | 4       | A            |
| 2       | 4       | A            |
| 3       | 5       | B            |

有没有办法连接两个表,其中右表中的匹配在SQL Server 2005中只连接一次?

我尝试了这个查询但没有成功:

SELECT * FROM myTable 
LEFT JOIN (SELECT * FROM myTable) AS t ON t.RelationalID = myTable.RelationalID 
WHERE myTable.Year = 2014 and t.Year = 2015

4 个答案:

答案 0 :(得分:2)

您可以根据ROW_NUMBERs获得结果,但是您需要一个规则如何分配它们,我假设它基于ID。

;WITH cte AS
(SELECT Id,
        RelationalId,
        year,
        row_number() 
        over (partition by RelationalId, year
              order by Id) as rn
FROM [YourTable] 
)
select t1.id as Id_2014,t2.id as Id_2015, t1.RelationalId
from cte as t1 left join cte as t2
on t1.RelationalId = t2.RelationalId
and t1.rn = t2.rn
and t2.year = 2015 
where t1.Year = 2014 

这是基于TMNT2014' fiddle

答案 1 :(得分:1)

在Sql下面会给你你想要的结果,但正如我之前所说,复杂性将取决于你在表中的原始数据集。这是SQL小提琴 - http://sqlfiddle.com/#!3/d6300/24 - 祝你好运!

;WITH CTE_Union AS
(SELECT 

    a.Id AS Id2014,
    NULL AS Id2015,
    a.RelationalId
  FROM [YourTable] a
  WHERE a.Year = 2014 
  UNION
  SELECT 
    NULL AS Id2014,
    b.Id AS Id2015,
    b.RelationalId
  FROM [YourTable] b
  WHERE b.Year = 2015)
SELECT Distinct CASE WHEN Id2014 IS NULL THEN (SELECT MIN(Id2014) FROM CTE_Union C WHERE    C.RelationalId =M.RelationalId) ELSE Id2014 END AS ID2014 , 
CASE WHEN Id2015 IS NULL AND Id2014 =  (SELECT MIN(Id2014) FROM CTE_Union C2 WHERE      C2.RelationalId =M.RelationalId) THEN (SELECT MIN(Id2015) FROM CTE_Union C WHERE       C.RelationalId =M.RelationalId) ELSE Id2015 END
,RelationalID
FROM CTE_Union M

答案 2 :(得分:1)

DECLARE @MyTable TABLE
 (
    ID INT,
    RelationalID VARCHAR(10),
    [Year] INT
)

INSERT INTO @MyTable
VALUES
( 1 ,'A', 2014),
( 2 ,'A', 2014),
( 3 ,'B', 2014),
( 4 ,'A', 2015),
( 5 ,'B', 2015)

;WITH TEST AS
(
    SELECT 
        a.Id AS Id2014,
        NULL AS Id2015,
        a.RelationalId,
        RANK() OVER (PARTITION BY RelationalId ORDER BY ID) Ranked
    FROM @MyTable a
    WHERE a.Year = 2014 
    UNION
    SELECT 
        NULL AS Id2014,
        b.Id AS Id2015,
        b.RelationalId,
        RANK() OVER (PARTITION BY RelationalId ORDER BY ID) Ranked
    FROM @MyTable b
    WHERE b.Year = 2015
)
SELECT
    t1.Id2014,
    t2.Id2015,
    t1.RelationalID
FROM TEST t1
LEFT JOIN TEST t2
    ON t1.Ranked = t2.Ranked
    AND t1.RelationalID = t2.RelationalID
    AND t2.Id2015 IS NOT NULL
WHERE t1.Id2014 IS NOT NULL
ORDER BY t1.Id2014

我使用了一个联盟,然后按关系id对每一方进行排名,并将其加入。 这是输出:

Id2014  Id2015  RelationalID
1   4   A
2   NULL    A
3   5   B

答案 3 :(得分:0)

可能有几种方法可以解决这个问题,但下面显示了一个利用"派生表"在查询中。

SELECT
  q1.Id AS [2014_Id],
  q2.Id AS [2015_Id],
  q1.RelationalId
FROM (SELECT 
        MAX(a.Id) AS Id,
        a.RelationalId
      FROM [table] a
      WHERE a.Year = 2014
      GROUP BY 
        a.RelationalId) q1
INNER JOIN (SELECT 
              MAX(b.Id) AS Id,
              b.RelationalId
            FROM [table] b
            WHERE b.Year = 2015
            GROUP BY 
                b.RelationalId) q2 
  ON q2.RelationalId = q1.RelationalId