连接 SQL 服务器中的表以获取所有数据

Joining tables in SQL Server to get all the data

我有 3 个table作为

Customer
Customer    |Name
1002        |ABB
2006        |Aberdeen C
.
.

和销售收入table

Customer|Inv.   |Product    |Sales Amount   |Gross Margin
1002    |600    |5      |8000       |125
1002    |601    |20     |0      |-10
1002    |602    |0      |320        |160
2006    |603    |7      |120        |0
.
.

应收账款table

Customer|Inv.   |Product    |Balance    |Line Amount    |Amount
1002    |500    |0      |150        |NULL       |150    
1002    |600    |5      |8000       |8000       |8000
1002    |601    |20     |0      |0      |0
1002    |602    |0      |0      |160        |160
2006    |603    |7      |120        |120        |0
.
.

我想通过加入销售收入和应收账款来创建客户视图 table 作为:

Customer|Name|Inv.|Product|Sales Amount |Gross Margin   |Balance    |Line Amount
1002    |ABB |500 |0      |NULL     |NULL       |150        |150
1002    |ABB |600 |5      |8000     |125        |8000       |8000
1002    |ABB |601 |20     |0        |-10        |0      |0
1002    |ABB |602 |0      |320      |160        |160        |160
2006    |ABB |603 |7      |120      |0      |120        |0
.
.

我试过了

CREATE  or alter   VIEW [dbo].[custmermasterview] as
SELECT [Customer]
      ,[Name]
      ,[Inv]
      ,[Sales Amount]
      ,[Gross Margin]
      ,[Balance]
      ,isnull([Line Amount],[Amount]) as LineAmount
      ,[dbo].[SalesRevenue].[product_key]

  FROM [PDI].[dbo].[Customer]
  LEFT JOIN [dbo].[SalesRevenue] ON 
   ([PDI].[dbo].[Customer].[customer_key] = [dbo].[SalesRevenue].[customer_key] and 
  LEFT JOIN [dbo].[AccountReceivables] ON
  ([dbo].[SalesRevenue].[Inv] = [dbo].[AccountReceivables].Inv)

显然,它与 SalesRevenue 合并,因此不会提取 AccountReceivables 中的记录。 如何获得所需的输出?

它有点难以测试,但我认为这会起作用

SELECT [Customer]
  ,[Name]
  ,[Inv]
  ,[Sales Amount]
  ,[Gross Margin]
  ,[Balance]
  ,isnull([Line Amount],[Amount]) as LineAmount
  ,[dbo].[SalesRevenue].[product_key]

  FROM [PDI].[dbo].[Customer]
  LEFT JOIN [dbo].[SalesRevenue] ON 
   ([PDI].[dbo].[Customer].[customer_key] = [dbo].[SalesRevenue].[customer_key]  
  LEFT JOIN [dbo].[AccountReceivables] ar ON
  ([dbo].[SalesRevenue].[Inv] = [dbo].[AccountReceivables].Inv)
  OR 
  ([PDI].[dbo].[Customer].[customer_key] = [dbo].[AccountReceivables].[customer] 
    AND NOT EXISTS(SELECT NULL FROM [dbo].[SalesRevenue] sr WHERE sr.[Inv] = ar.[Inv] and 
    sr.[customer] = ar.[customer] ) )

我为表添加了一些别名,以便更容易加入。另外,您的查询使用 customer_key 加入,但您的表说 customer?

您可以添加一个从两个表中获取所有发票的子查询并加入该子查询:

SELECT [Customer]
      ,[Name]
      ,[Inv]
      ,[Sales Amount]
      ,[Gross Margin]
      ,[Balance]
      ,isnull([Line Amount],[Amount]) as LineAmount
      ,[dbo].[SalesRevenue].[product_key]

  FROM [PDI].[dbo].[Customer]
  JOIN (    SELECT DISTINCT [customer_key] , inv, product
        FROM [dbo].[AccountReceivables]
        UNION
        SELECT DISTINCT [customer_key] , inv, product
        FROM [dbo].[SalesRevenue]
    ) all
  LEFT JOIN [dbo].[SalesRevenue] ON 
    [dbo].[SalesRevenue].[customer_key] = all.[customer_key]  
    and [SalesRevenue].inv = all.inv
    and [SalesRevenue].product = all.product 

  LEFT JOIN [dbo].[AccountReceivables] ON
    all.[Inv] = [dbo].[AccountReceivables].Inv
    and [AccountReceivables].inv = all.inv
    and [AccountReceivables].product = all.product