在sql server中加入两个视图

时间:2016-07-14 11:19:32

标签: sql sql-server view

view1

cal286Darkt123

视图2

+------+---------------+    
|PERSON  | CONTACTLIST |   
+------+---------------+ 
| A  |      CONT1      |   
| A  |      CONT2      |
| A  |      CONT5      |
| B  |      CONT1      |
| B  |      CONT3      |
| C  |      CONT2      |
+------+---------------+   

通缉结果

+------+------------------+    
|PERSON  | MEETINGS WITH  |   
+------+------------------+ 
| A  |      CONT7         |   
| A  |      CONT8         |
| A  |      CONT1         |
| A  |      CONT2         |
| B  |      CONT1         |
| B  |      CONT3         |
| B  |      CONT4         |
| C  |      CONT1         |
| C  |      CONT2         |
| C  |      CONT3         |
| C  |      CONT4         |
| C  |      CONT5         |
+------+---------------+   

我有“查看1”包含每个人的联系人列表! 一个人可以遇到多个联系人,会议可以在他的联系人列表中与一个联系人联系!

我想张贴一个标志,表明会议是否与联系人列表中的联系人有关!

2 个答案:

答案 0 :(得分:1)

这样的事情

select v2.PERSON, 
       v2.[MEETINGS WITH],
       case when v1.PERSON is null then 0 else 1 end as FLAG
from   view2 v2
  left outer join view1 v1 on v2.PERSON = v1.PERSON and v2.[MEETING WITH] = v1.CONTACTLIST

答案 1 :(得分:1)

试试这个

SELECT 
    V.PERSON,
    V.[MEETINGS WITH],
    (
        SELECT COUNT(1) 
        FROM 
            view1 
        WHERE 
            view1.PERSON = V.PERSON AND 
            view1.CONTACTLIST = V.[MEETINGS WITH]
     ) Counts
 FROM
     View2 V