简体   繁体   English

FULL OUTER JOIN的值出现在2个不同的字段中

[英]Values of FULL OUTER JOIN appearing in 2 different fields

here is my query: 这是我的查询:

SELECT
COALESCE ([dbo].[RSA_BIRMINGHAM_1941$].TOS,
[dbo].[RSA_CARDIFFREGUS_2911$].TOS,[dbo].[RSA_CASTLEMEAD_1941$].TOS,
[dbo].[RSA_CHELMSFORD_1941$].TOS) AS [TOS Value]

,RSA_BIRMINGHAM_1941$.Percentage            AS [Birmingham]
,RSA_CARDIFFREGUS_2911$.Percentage          AS [Cardiff Regus]
,[dbo].[RSA_CASTLEMEAD_1941$].Percentage    AS [Castlemead]
,[dbo].[RSA_CHELMSFORD_1941$].Percentage    AS [Chelmsford]

FROM [dbo].[RSA_BIRMINGHAM_1941$] 
FULL OUTER JOIN [dbo].[RSA_CARDIFFREGUS_2911$] 
ON [dbo].[RSA_BIRMINGHAM_1941$].TOS = [dbo].[RSA_CARDIFFREGUS_2911$].TOS

FULL OUTER JOIN [dbo].[RSA_CASTLEMEAD_1941$] 
ON [dbo].[RSA_BIRMINGHAM_1941$].TOS = [dbo].[RSA_CASTLEMEAD_1941$].TOS

FULL OUTER JOIN [dbo].[RSA_CHELMSFORD_1941$] 
ON [dbo].[RSA_BIRMINGHAM_1941$].TOS = [dbo].[RSA_CHELMSFORD_1941$].TOS

And here is the output: 这是输出:

TOS Value        Birmingham   Cardiff Regus   Castlemead    Chelmsford
default (DSCP 0)   61.37%       61.74%         99.48%         79.78%
af11 (DSCP 10)     15.22%       4.63%          0.00%          6.16%
af33 (DSCP 30)     11.49%       15.44%         NULL           7.33%
af31 (DSCP 26)     8.86%        13.85%         0.01%          5.59%
ef (DSCP 46)       1.91%        3.72%          0.49%          0.91%
af41 (DSCP 34)     0.70%        0.03%          0.01%          0.05%
cs4 (DSCP 32)      0.15%        0.20%          NULL           0.10%
af12 (DSCP 12)     0.12%        NULL           NULL           NULL
cs3 (DSCP 24)      0.06%        0.11%          0.01%          0.04%
af21 (DSCP 18)     0.05%        0.05%          0.00%          0.02%
cs6 (DSCP 48)      NULL         0.23%          NULL           NULL
cs6 (DSCP 48)      NULL         NULL           0.00%          NULL
af32 (DSCP 28)     NULL         NULL           NULL           0.02%

If you have a look at the TOS column and look at value cs6 (DSCP 48) you will see that it has been duplicated. 如果您查看“ TOS”列并查看值cs6(DSCP 48),您将看到它已重复。 there should be only 1 cs6 (DSCP 48) row but for some reason the Castlemead value (0.00%) for cs6 (DSCP 48) has been created as a separate column. 只能有1个cs6(DSCP 48)行,但是由于某种原因cs6(DSCP 48)的Castlemead值(0.00%)已创建为单独的列。

there should be only one row per TOS value if that makes sense so please tell where did I go wrong? 如果可以的话,每个TOS值应该只有一行,所以请告诉我我哪里出错了?

The results you get are as expected. 您得到的结果符合预期。 This is because the joins are all relative to the first table. 这是因为联接都相对于第一个表。 If there is a TOS in the second table that has no match with the first table that will generate a new record. 如果第二个表中的TOS与第一个表不匹配,则将生成新记录。 If there is a TOS in the third table that has no match with the first table that will again generate a new record. 如果第三个表中的TOS与第一个表不匹配,则会再次生成新记录。 There is no clue for the engine to know that such instances should be combined into one result. 引擎不知道应该将此类实例合并为一个结果。

There are probably several ways to resolve this. 可能有几种方法可以解决此问题。 I will suggest one where you introduce a UNION sub select that will combine all TOS values, and then an INNER JOIN to each of the four tables. 我将建议您在其中引入一个UNION子选择,该子选择将结合所有TOS值,然后INNER JOIN四个表中的每个表进行INNER JOIN

SELECT       REF.TOS AS [TOS Value]
            ,RSA_BIRMINGHAM_1941$.Percentage    AS [Birmingham]
            ,RSA_CARDIFFREGUS_2911$.Percentage  AS [Cardiff Regus]
            ,RSA_CASTLEMEAD_1941$.Percentage    AS [Castlemead]
            ,RSA_CHELMSFORD_1941$.Percentage    AS [Chelmsford]

FROM       ( SELECT TOS FROM RSA_BIRMINGHAM_1941$   UNION
             SELECT TOS FROM RSA_CARDIFFREGUS_2911$ UNION
             SELECT TOS FROM RSA_CASTLEMEAD_1941$   UNION
             SELECT TOS FROM RSA_CHELMSFORD_1941$
           ) AS REF
INNER JOIN RSA_BIRMINGHAM_1941$   ON REF.TOS = RSA_BIRMINGHAM_1941$.TOS
INNER JOIN RSA_CARDIFFREGUS_2911$ ON REF.TOS = RSA_CARDIFFREGUS_2911$.TOS
INNER JOIN RSA_CASTLEMEAD_1941$   ON REF.TOS = RSA_CASTLEMEAD_1941$.TOS
INNER JOIN RSA_CHELMSFORD_1941$   ON REF.TOS = RSA_CHELMSFORD_1941$.TOS

Queries are so much easier to write and to read with table aliases. 使用表别名,查询的编写和读取非常容易。 The problem is the matching in the second FULL OUTER JOIN . 问题是第二个FULL OUTER JOIN的匹配。 The FROM clause needs to look like this: FROM子句需要看起来像这样:

FROM [dbo].[RSA_BIRMINGHAM_1941$] b FULL OUTER JOIN
     [dbo].[RSA_CARDIFFREGUS_2911$] cr
     ON b.TOS = cr.TOS FULL OUTER JOIN
     [dbo].[RSA_CASTLEMEAD_1941$] cm
     ON cm.TOS IN (b.TOS, cr.TOS) FULL OUTER JOIN
     [dbo].[RSA_CHELMSFORD_1941$] cf
     ON cf.TOS IN (b.TOS, cr.TOS, cm.TOS)

In other words, by comparing to only one TOS field in the later joins, you might be joining to an unmatched column -- and hence getting a duplicate. 换句话说,通过仅与后面的联接中的一个TOS字段进行比较,您可能正在联接至一个不匹配的列-从而得到了重复项。 One FULL OUTER JOIN is fine. 一个FULL OUTER JOIN就可以了。 Multiple FULL OUTER JOIN s are tricky. 多个FULL OUTER JOIN很难。 I often use UNION ALL queries instead. 我经常改用UNION ALL查询。

Thanks to @trincot as he is the one who layed the fondation to the outcome that i was looking for. 感谢@trincot,因为他是对我一直在寻找的结果表示满意的人。 here is the query that has solved my issue: 这是解决了我的问题的查询:

SELECT       REF.TOS AS [TOS Value]
        ,RSA_BIRMINGHAM_1941$.Percentage    AS [Birmingham]
        ,RSA_CARDIFFREGUS_2911$.Percentage  AS [Cardiff Regus]
        ,RSA_CASTLEMEAD_1941$.Percentage    AS [Castlemead]
        ,RSA_CHELMSFORD_1941$.Percentage    AS [Chelmsford]

 FROM    
         (SELECT TOS FROM RSA_BIRMINGHAM_1941$   UNION
         SELECT TOS FROM RSA_CARDIFFREGUS_2911$ UNION
         SELECT TOS FROM RSA_CASTLEMEAD_1941$   UNION
         SELECT TOS FROM RSA_CHELMSFORD_1941$) AS REF

       FULL OUTER JOIN RSA_BIRMINGHAM_1941$   ON REF.TOS =  
       RSA_BIRMINGHAM_1941$.TOS
       FULL OUTER JOIN RSA_CARDIFFREGUS_2911$ ON REF.TOS =
       RSA_CARDIFFREGUS_2911$.TOS
       FULL OUTER JOIN RSA_CASTLEMEAD_1941$   ON REF.TOS = 
       RSA_CASTLEMEAD_1941$.TOS
       FULL OUTER JOIN RSA_CHELMSFORD_1941$   ON REF.TOS = 
       RSA_CHELMSFORD_1941$.TOS

       ORDER BY [TOS Value]

and the result is the following as expected: 结果是预期的以下结果:

TOS Value      Birmingham   CardiffRegus    Castlemead  Chelmsford
af11 (DSCP 10)  15.22%      4.63%           0.00%        6.16%
af12 (DSCP 12)  0.12%       NULL            NULL         NULL
af21 (DSCP 18)  0.05%       0.05%           0.00%        0.02%
af31 (DSCP 26)  8.86%       13.85%          0.01%        5.59%
af32 (DSCP 28)  NULL        NULL            NULL         0.02%
af33 (DSCP 30)  11.49%      15.44%          NULL         7.33%
af41 (DSCP 34)  0.70%       0.03%           0.01%        0.05%
cs3 (DSCP 24)   0.06%       0.11%           0.01%        0.04%
cs4 (DSCP 32)   0.15%       0.20%           NULL         0.10%
cs6 (DSCP 48)   NULL        0.23%           0.00%        NULL
defau(DSCP 0)   61.37%      61.74%          99.48%       79.78%
ef (DSCP 46)    1.91%       3.72%           0.49%        0.91%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM