【问题标题】:Joining tables in SQL Server to get all the data在 SQL Server 中连接表以获取所有数据
【发布时间】:2020-05-29 08:30:24
【问题描述】:

我有 3 张桌子

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

销售收入表

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
.
.

应收账款表

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
.
.

我想通过加入销售收入和应收帐款表来创建客户视图:

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 的记录。 如何获得所需的输出?

【问题讨论】:

    标签: sql-server join multiple-tables


    【解决方案1】:

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

    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 
    

    【讨论】:

      【解决方案2】:

      这有点难测试,但我认为这会奏效

      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_key,为了简化,我提到了customer。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多