在 3 个表上使用 LEFT JOIN 时生成 2 列

Generate 2 columns when using LEFT JOIN on 3 tables

我有 3 个 table:

呼叫table:

    +-----------+-------------+
    | CallId    | CallerName  |
    +-----------+-------------+
    | A         | Alice       |
    | B         | Bruno       |    
    | C         | Tina        |   
    | D         | Marina      | 
    | E         | Elena       |        
    +-----------+-------------+

已接电话table

   +-----------+-------------+
   | CallId    | PhoneNumber |
   +-----------+-------------+
   | A         | 1234567     |
   | B         | 45678       |    
   | C         | 1598        |           
   +-----------+-------------+

AllCallsDetail table:

+--------+--------+-------+-------------+
| CallId | Caller | Callee| CallType    |
+--------+--------+-------+-------------+
| A      |   1234 |  1111 |        1    |
| B      |   4567 |  2222 |        2    |
| C      |   8927 |    -1 |        2    |
| D      |   4567 |  2562 |        2    |
| E      |   8927 |    -1 |        1    |
+--------+--------+-------+-------------+

我必须创建一个视图,其中包含来自 Call table 的所有录音,加入 AnsweredCall table(存在值,不存在,显示 null )并加入 AllCallsDetails 我必须显示两个按 CallTypeId 筛选的列。
第一列是这样的:

(SELECT allCalls.Caller Where (allCalls.CallType= 1)) as Caller

第二列如下:

(SELECT allCalls.Callee Where (allCalls.ConferenceType!= 1 and  allCalls.Callee != '-1')) as Callee

现在我有这样的东西,但是 Callee 列总是空的,我不明白为什么,因为 Caller 列包含好的值。

 Create VIEW [dbo].[CallView]
           as 
        Select      NEWID() As Id
                    ,callTbl.CallId
                    ,callTbl.CallerName
                    ,answeredCallTbl.PhoneNumber
                    ,(SELECT callDetailTbl.Caller Where(callDetailTbl.CallType = 1)) as Caller
                    ,(SELECT callDetailTbl.Callee Where (callDetailTbl.CallType != 1 and  callDetailTbl.Callee != '-1')) as Callee

     FROM [dbo].[Call] as callTbl
     left join [dbo].[AnsweredCall] answeredCallTbl on callTbl.CallId= answeredCallTbl .CallId
     left  join [dbo].[AllCallsDetails] callDetailTbl on callTbl.CallId= callDetailTbl .CallId

输出必须如下所示:

+----+--------+------------+-------------+--------+-------+
| Id | CallId | CallerName | PhoneNumber | Caller | Calle |
+----+--------+------------+-------------+--------+-------+
|  1 | A      | Alice      | 1234567     | 1234   | null  |
|  2 | B      | bruno      | 45678       | null   | 2222  |
|  3 | C      | tina       | 1598        | null   | null  |
|  4 | D      | Marina     | null        | null   | 2562  |
|  5 | E      | Elena      | null        | 8927   | null  |
+----+--------+------------+-------------+--------+-------+

您可能可以使用 CASE 声明,例如

 Create VIEW [dbo].[CallView]
           as 
    Select      NEWID() As Id
                ,callTbl.CallId
                ,callTbl.CallerName
                ,answeredCallTbl.PhoneNumber
                ,case when coalesce(callDetailTbl.CallType, 0) = 1 then callDetailTbl.Caller end as Caller
                ,case when (coalesce(callDetailTbl.CallType,0) != 1 and  coalesce(callDetailTbl.Callee, '') != '-1') then callDetailTbl.Callee end as Callee

 FROM [Call] as callTbl
 left join [AnsweredCall] answeredCallTbl on callTbl.CallId= answeredCallTbl .CallId
 left  join [AllCallsDetail] callDetailTbl on callTbl.CallId= callDetailTbl .CallId

看到一个Demo Fiddle