3

我正在连接三个表(执行完全外部连接),以便可以从所有表中检索所有记录。我面临的问题是我加入表格的顺序。

表信息

替代文字 http://img235.imageshack.us/img235/7980/tableinfoow1.png

(1) 如果我以 TABLE1、TABLE2、TABLE3 的顺序连接表,我会得到两行记录,用于团队 B 和级别 1。

SELECT DISTINCT 
    (CASE WHEN T0.[TEAM] IS NOT NULL THEN T0.[TEAM] WHEN T1.[TEAM] IS NOT NULL THEN T1.[TEAM] WHEN T2.[TEAM] IS NOT NULL THEN T2.[TEAM] ELSE T0.[TEAM] END) AS [TEAM], 
    (CASE WHEN T0.[LEVEL] IS NOT NULL THEN T0.[LEVEL] WHEN T1.[LEVEL] IS NOT NULL THEN T1.[LEVEL] WHEN T2.[LEVEL] IS NOT NULL THEN T2.[LEVEL] ELSE T0.[LEVEL] END) AS [LEVEL], 
    T0.[VALUE1] AS [VALUE1], 
    T1.[VALUE2] AS [VALUE2], 
    T2.[VALUE3] AS [VALUE3] 

FROM TABLE1 T0
FULL JOIN TABLE2 T1 ON T0.[TEAM] = T1.[TEAM] AND T0.[LEVEL] = T1.[LEVEL] 
FULL JOIN TABLE3 T2 ON T0.[TEAM] = T2.[TEAM] AND T0.[LEVEL] = T2.[LEVEL]

(2) 如果我在 TABLE2、TABLE3、TABLE1 序列中加入表,我会在输出中得到正确的行数。

SELECT DISTINCT 
    (CASE WHEN T0.[TEAM] IS NOT NULL THEN T0.[TEAM] WHEN T1.[TEAM] IS NOT NULL THEN T1.[TEAM] WHEN T2.[TEAM] IS NOT NULL THEN T2.[TEAM] ELSE T0.[TEAM] END) AS [TEAM], 
    (CASE WHEN T0.[LEVEL] IS NOT NULL THEN T0.[LEVEL] WHEN T1.[LEVEL] IS NOT NULL THEN T1.[LEVEL] WHEN T2.[LEVEL] IS NOT NULL THEN T2.[LEVEL] ELSE T0.[LEVEL] END) AS [LEVEL], 
    T0.[VALUE1] AS [VALUE1], 
    T1.[VALUE2] AS [VALUE2], 
    T2.[VALUE3] AS [VALUE3] 

FROM TABLE2 T0
FULL JOIN TABLE3 T1 ON T0.[TEAM] = T1.[TEAM] AND T0.[LEVEL] = T1.[LEVEL] 
FULL JOIN TABLE1 T2 ON T0.[TEAM] = T2.[TEAM] AND T0.[LEVEL] = T2.[LEVEL]

我面临的问题是我不知道输入表并在运行时将所有这些表作为用户的输入并执行连接。我不能一次合并两个表,因为从技术上讲,我的表一次可以合并三个以上的表(最多 9 个或 10 个)。

如何确保从所有表中获取所有记录(使用完全外连接),但不要像 #1 中那样获取两行。

4

4 回答 4

3

如果这是您需要的:

TEAM LEVEL  Value1  Value2  Value3
A   1        1       NULL    NULL
B   1        NULL    1000    900

然后,您可以通过以下方式实现:

SELECT [TEAM], [LEVEL], MAX(v1) Value1, MAX(v2) Value2, MAX(v3) Value3
FROM (
    SELECT [TEAM], [LEVEL], Value1 v1, NULL v2, NULL v3
    FROM TABLE1
    UNION
    SELECT [TEAM], [LEVEL], NULL, Value2, NULL
    FROM TABLE2
    UNION
    SELECT [TEAM], [LEVEL], NULL, NULL, Value3
    FROM TABLE3
) t0
GROUP BY [TEAM], [LEVEL]

您可以根据需要使用任意数量的表格。

于 2009-01-14T12:50:29.577 回答
0

您需要在第二个连接表达式上添加一个额外的匹配条件,以允许它匹配第二个表的团队/级别值。我还使用 ISNULL 简化了团队/级别列表达式:

SELECT DISTINCT 
    ISNULL(T0.[TEAM],ISNULL(T1.[TEAM],T2.[TEAM])) AS [TEAM],
    ISNULL(T0.[LEVEL],ISNULL(T1.[LEVEL],T2.[LEVEL])) AS [LEVEL],
    T0.[VALUE1], T1.[VALUE2], T2.[VALUE3]     
FROM TABLE1 T0
FULL JOIN TABLE2 T1 ON T0.[TEAM] = T1.[TEAM] AND T0.[LEVEL] = T1.[LEVEL] 
FULL JOIN TABLE3 T2 ON (T0.[TEAM] = T2.[TEAM] AND T0.[LEVEL] = T2.[LEVEL])
                    OR (T1.[TEAM] = T2.[TEAM] AND T1.[LEVEL] = T2.[LEVEL])

看?T1 没有与 T2 显示在同一行,因为您从未允许将其作为匹配的可能性。

于 2009-09-24T20:58:39.790 回答
0

这就是 FULL OUTER JOIN 的定义(当使用它时,几乎总是表明设计不佳 - 我大约每年使用一次 FULL OUTER JOIN)。

也许如果你给出了你正在寻找的结果?

我在想 UNION、GROUP BY 和 COALESCE。

于 2009-01-14T04:54:34.017 回答
0

正如你在 yru 查询中定义的不同,这意味着在这个联合中沿着一个有 r 多个条目都是最好的,因为数据中有重复。

SELECT [TEAM], [LEVEL], MAX(v1) Value1, MAX(v2) Value2, MAX(v3) Value3
FROM (
    SELECT [TEAM], [LEVEL],  v1, NULL v2, NULL v3
    FROM TABLE1
    UNION ALL
    SELECT [TEAM], [LEVEL], NULL v1, V2, NULL v3
    FROM TABLE2
    UNION ALL
    SELECT [TEAM], [LEVEL], NULL V1, NULL V2, V3
    FROM TABLE3
) t0
GROUP BY [TEAM], [LEVEL]
于 2013-03-20T11:09:06.270 回答