将子表联接到主表

时间:2019-02-25 10:21:06

标签: sql sql-server

我有三个声明的表:

  • 表_1(默认)具有与子表_2和表_3相同的组号
  • 而Table_2和Table_3具有相同的日期

我很难将两个子表都连接到T1,而第一次连接后又不会丢失数据。加入T2之后,T1仍应具有其初始数据,而不是NULL

我尝试过:

SELECT *
FROM (
        SELECT DISTINCT * from @GRP_TABLE as T1
        FULL OUTER JOIN
        (SELECT * FROM @PASSEDBOOKINGS) AS T2
        on T1.T1_GROUP_NUMBER = T2.T2_GROUP_NUMBER  
       FULL OUTER JOIN
        (SELECT * FROM @FAILBOOKINGS)AS T3
        ON T3.T3_GROUP_NUMBER = T1.T1_GROUP_NUMBER
         and T3.T3_DATE = T2.T2_DATE
    )AS JOINS

WHERE JOINS.T3_DATE = CAST(GETDATE() AS DATE) 
OR JOINS.T2_DATE =  CAST(GETDATE() AS DATE)

@GRP_TABLE AS T1:

|-------|------------------|
|T1_LINE| T1_GROUP_NUMBER  | 
|-------|------------------|
|  A1   |        A110      |
|-------|------------------|
|  A1   | A120             |
|-------|------------------|
|A1     |A130              |
|-------|------------------|
|A1     |A140              |
|-------|------------------|
|A1     |A150              |
|-------|------------------|

@通行证为T2:

|---------------|-----------|----------|
|T2_GROUP_NUMBER| T2_COUNTS | T2_DATE  |
|---------------|-----------|----------|
|  A110         | 2         |2019-02-25|
|---------------|-----------|----------|
|  A120         | 2         |2019-02-25|
|---------------|-----------|----------|
|A130           |2          |2019-02-25|
|---------------|-----------|----------|

@FAILEDBOOKINGS AS T3:

|---------------|-----------|----------|
|T3_GROUP_NUMBER| T3_COUNTS | T3_DATE  |
|---------------|-----------|----------|
|  A140         | 1         |2019-02-25|
|---------------|-----------|----------|
|  A150         | 1         |2019-02-25|
|---------------|-----------|----------|

结果是:

|-------|------------------|------------------|----------|-----------------|-------------|------------------|------------------|
|T1_LINE| T1_GROUP_NUMBER  | T2_GROUP_NUMBER  |T2_COUNTS | T3_GROUP_NUMBER |T3_COUNTS    |      T2_DATE     | T3_DATE          |
|-------|------------------|------------------|----------|-----------------|-------------|------------------|------------------|
|  NULL |        NULL      |        NULL      |     NULL |  A140           |       1     |      NULL        |   2019-02-25     |
|-------|------------------|------------------|----------|-----------------|-------------|------------------|------------------|
|  NULL | NULL             |          NULL    | NULL     |A150             |        1    |      NULL        |   2019-02-25     |
|-------|------------------|------------------|----------|-----------------|-------------|------------------|------------------|
|A1     |A110              |    A110          |     2    |      NULL       |    NULL     |      2019-02-25  |   NULL           |
|-------|------------------|------------------|----------|-----------------|-------------|------------------|------------------|
|A1     |A120              |    A120          |     2    |      NULL       |    NULL     |      2019-02-25  |   NULL           |
|-------|------------------|------------------|----------|-----------------|-------------|------------------|------------------|
|A1     |A130              |    A130          |     2    |      NULL       |    NULL     |      2019-02-25  |       NULL       |
|-------|------------------|------------------|----------|-----------------|-------------|------------------|------------------|

但是我期望这些结果:

|-------|------------------|------------------|----------|-----------------|-------------|------------------|------------------|
|T1_LINE| T1_GROUP_NUMBER  | T2_GROUP_NUMBER  |T2_COUNTS | T3_GROUP_NUMBER |T3_COUNTS    |      T2_DATE     | T3_DATE          |
|-------|------------------|------------------|----------|-----------------|-------------|------------------|------------------|
|  A1   |        A140      |        NULL      |     NULL |  A140           |       1     |      NULL        |   2019-02-25     |
|-------|------------------|------------------|----------|-----------------|-------------|------------------|------------------|
|  A1   | A150             |          NULL    | NULL     |A150             |        1    |      NULL        |   2019-02-25     |
|-------|------------------|------------------|----------|-----------------|-------------|------------------|------------------|
|A1     |A110              |    A110          |     2    |      NULL       |    NULL     |      2019-02-25  |   NULL           |
|-------|------------------|------------------|----------|-----------------|-------------|------------------|------------------|
|A1     |A120              |    A120          |     2    |      NULL       |    NULL     |      2019-02-25  |   NULL           |
|-------|------------------|------------------|----------|-----------------|-------------|------------------|------------------|
|A1     |A130              |    A130          |     2    |      NULL       |    NULL     |      2019-02-25  |       NULL       |
|-------|------------------|------------------|----------|-----------------|-------------|------------------|------------------|

4 个答案:

答案 0 :(得分:2)

您可以尝试使用COALESCE函数-因此,如果一个表中没有数据,请从另一个表中将其提取

谢谢

答案 1 :(得分:1)

输出为空的原因

  1. 即使T1表包含A140和A150,相应的T2和T3日期也不相同,实际上样本数据中的日期不存在。

要获得预期的效果,可以执行以下操作,

SELECT *
From
 @GRP_TABLE as T1
Left JOIN
 @PASSEDBOOKINGS AS T2
on T1.T1_GROUP_NUMBER = T2.T2_GROUP_NUMBER  
Left JOIN
@FAILBOOKINGS AS T3
        ON T3.T3_GROUP_NUMBER = T1.T1_GROUP_NUMBER
WHERE T3.T3_DATE = CAST(GETDATE() AS DATE) 
OR T2.T2_DATE =  CAST(GETDATE() AS DATE)

答案 2 :(得分:1)

查看数据似乎需要左联接

SELECT DISTINCT T1.T1_LINE, T1.T1_GROUP_NUMBER
    ,  T2.T2_GROUP_NUMBER , T2.T2_COUNT
    , T3.T3_GROUP_NUMBER, T2.T2_COUNTS,  T2.T2_DATE, T3.T3_DATE, T3.T3_COUNT 
from @GRP_TABLE T1 
LEFT JOIN @PASSEDBOOKING T2 ON T1.T1_GROUP_NUMBER = T2.T2_GROUP_NUMBER 
LEFT JOIN @FAILEDBOOKINGS AS T3  ON T1.T1_GROUP_NUMBER = T3.T2_GROUP_NUMBER  

答案 3 :(得分:1)

FULL OUTER JOIN非常昂贵,在这里我没有理由使用它。您有一个始终要返回数据的主表和两个辅助表-这是FROM T1左联接T2,T3的经典案例...

请改用此方法。

    SELECT *
FROM @GRP_TABLE as T1
LEFT JOIN @PASSEDBOOKINGS AS T2
        on T1.T1_GROUP_NUMBER = T2.T2_GROUP_NUMBER  
        and T2.T2_DATE =  CAST(GETDATE() AS DATE)
LEFT JOIN  @FAILBOOKINGS AS T3
        ON T3.T3_GROUP_NUMBER = T1.T1_GROUP_NUMBER
         and T3.T3_DATE = T2.T2_DATE

考虑到两个表的日期都限制为一个日期,我什至不知道您是否必须加入日期。

SELECT *
FROM @GRP_TABLE as T1
LEFT JOIN @PASSEDBOOKINGS AS T2
        on T1.T1_GROUP_NUMBER = T2.T2_GROUP_NUMBER  
        and T2.T2_DATE =  CAST(GETDATE() AS DATE)
LEFT JOIN  @FAILBOOKINGS AS T3
        ON T3.T3_GROUP_NUMBER = T1.T1_GROUP_NUMBER
         and T3.T3_DATE = CAST(GETDATE() AS DATE)

还考虑将T2和T3合并,可能更有意义!

SELECT *
FROM @GRP_TABLE as T1
LEFT JOIN (SELECT 'pass' as type, * FROM @PASSEDBOOKINGS WHERE DATE = CAST(GETDATE() AS DATE
                UNION
            SELECT 'fail' as type, * FROM @FAILBOOKINGS WHERE DATE = CAST(GETDATE() AS DATE
            ) 
            T2 on T2.GROUP_NUMBER = T1.GROUP_NUMBER