代码之家  ›  专栏  ›  技术社区  ›  Michael Grassman

我可以使用哪种类型的连接来复制这些结果

  •  0
  • Michael Grassman  · 技术社区  · 15 年前

    我有以下包含此数据的视图

    ActivityRecId RegionRecId IsExcluded
    1             null        1
    2             null        1
    3             1           1
    3             2           1
    4             1           1
    5             null        0
    

    我要做的是将区域表连接到上面的视图以获取以下记录。

    ActivityRecId RegionRecId IsExcluded
    1             null        1
    2             null        1
    3             1           1
    3             2           1
    3             3           0
    3             4           0
    4             1           1
    4             2           0
    4             3           0
    4             4           0
    5             null        0
    

    区域表具有以下列:

    • 区域接收
    • 地区名称

    有什么建议吗?如果您需要其他信息,请告诉我。

    --------------------- CORRECT QUESTION ------------------------
    ActivityRecId RegionRecId IsExcluded
        1             null        1
        2             null        1
        3             1           1
        3             2           1
        3             3           0
        3             4           0
        4             1           1
        4             2           0
        4             3           0
        4             4           0
        5             1           0
        5             2           0
        5             3           0
        5             4           0
    

    如果这样做更容易,活动1和2也可以列出所有区域。

    谢谢,

    2 回复  |  直到 6 年前
        1
  •  0
  •   Rich    15 年前

    我没有现成的SQL Server来测试这个,但是

    select  *
    from    myView
    
    union
    
    select  myView.ActivityRecId,
            region.RegionRecId,
            0 as IsExcluded
    from    myView cross join region
    where   (myView.RegionRecId is not null or myView.IsExcluded = 0)
            and not exists (
                select  null
                from    myView
                where   myView.RegionRecId = region.RegionRecId
            )
    

    你想要什么?

        2
  •  0
  •   Cœur Gustavo Armenta    6 年前

    Here 我们可以参考一下Join。我想你需要一个左外接