2009-12-21 34 views
0

我有以下看法包含该数据我可以使用哪种类型的联接重现这些结果

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 

区表有以下栏目:

  • RegionRecId
  • RegionName

任何建议。让我知道你是否需要任何其他信息。

--------------------- 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也可以列出所有的区域。

感谢,

+1

区域表包含什么?你展示的东西看起来更像联合而不是联结。 – Rich 2009-12-21 19:55:24

+0

region表只显示RegionRecId和RegionName – 2009-12-21 19:56:35

+1

没有意义 - 对于提供的内容,表之间没有联接。您需要视图的所有输出,然后列出视图中不存在的区域表中的regionrecid。 – 2009-12-21 20:09:08

回答

0

我没有一个SQL服务器方便对此进行测试,但会像

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 
     ) 

是你想要的吗?

+0

这是我给你的东西。谢谢您的回答。你能否修改更新的问题。 – 2009-12-21 20:46:20

+0

我希望我的修改后的解决方案现在正确无误,但我再次编写查询时未能对其进行测试。 – Rich 2009-12-21 21:58:38

0

Here的伟大参考看看的连接。我想你需要左外连接

相关问题