0

我正在尝试创建一个包含多个表中的列的视图。

这是它的样子:

在此处输入图像描述

这是我的查询:

SELECT     
   Billing.WebPortalBilling.WebPortalBillingId, 
   Billing.WebPortalBilling.CorporationId, 
   Billing.WebPortalBilling.TokenId, 
   Billing.WebPortalBilling.GatewaySupportFee, 
   Billing.WebPortalBilling.GatewayPerTransactionFee, 
   Billing.WebPortalBilling.PortalPerCustomerFee, 
   Billing.WebPortalBilling.PortalSupportFee, 
   Customer.Account.AccountNumber, 
   Billing.WebPortalBilling.IsActive, 
   Customer.Customer.Name, 
   Customer.Customer.TaxCode, 
   Company.CorporationStructure.Branch
FROM         
   Company.CorporationStructure 
RIGHT OUTER JOIN
   Customer.Account ON Company.CorporationStructure.CorporationStructureId = Customer.Account.CorporationStructureId 
RIGHT OUTER JOIN
   Customer.Customer ON Company.CorporationStructure.Branch = Customer.Customer.Branch 
RIGHT OUTER JOIN
   Billing.WebPortalBilling ON Customer.Account.CorporationId = Billing.WebPortalBilling.CorporationId
WHERE     
   (Billing.WebPortalBilling.IsActive = 1)

它只返回 1 条记录,这是不正确的。我正在尝试将客户的姓名与其他两个表中的帐号和名称一起绑定到 WebPortalBilling 表。

我是 sql 新手,所以请善待。

谢谢!

4

2 回答 2

2

正如所评论的那样,在哪里杀死了外部
尝试

SELECT     
   Billing.WebPortalBilling.WebPortalBillingId, 
   Billing.WebPortalBilling.CorporationId, 
   Billing.WebPortalBilling.TokenId, 
   Billing.WebPortalBilling.GatewaySupportFee, 
   Billing.WebPortalBilling.GatewayPerTransactionFee, 
   Billing.WebPortalBilling.PortalPerCustomerFee, 
   Billing.WebPortalBilling.PortalSupportFee, 
   Customer.Account.AccountNumber, 
   Billing.WebPortalBilling.IsActive, 
   Customer.Customer.Name, 
   Customer.Customer.TaxCode, 
   Company.CorporationStructure.Branch
FROM         
   Company.CorporationStructure 
RIGHT OUTER JOIN
   Customer.Account ON Company.CorporationStructure.CorporationStructureId = Customer.Account.CorporationStructureId 
RIGHT OUTER JOIN
   Customer.Customer ON Company.CorporationStructure.Branch = Customer.Customer.Branch 
RIGHT OUTER JOIN  Billing.WebPortalBilling 
    ON Customer.Account.CorporationId = Billing.WebPortalBilling.CorporationId
   AND Billing.WebPortalBilling.IsActive = 1
于 2013-04-05T15:35:18.770 回答
0

试试这个,我认为左连接更清晰。

SELECT    
  B.WebPortalBillingId, 
  B.CorporationId,   
  B.TokenId, 
  B.GatewaySupportFee, 
  B.GatewayPerTransactionFee, 
  B.PortalPerCustomerFee, 
  B.PortalSupportFee, 
  C.AccountNumber, 
  B.IsActive, 
  C.Name,
  C.TaxCode, 
  CS.Branch
FROM  Customer.Customer C
  LEFT JOIN  Company.CorporationStructure CS ON CS.Branch = C.Branch 
  LEFT JOIN  Customer.Account A ON CS.CorporationStructureId = A.CorporationStructureId
  LEFT JOIN  Billing.WebPortalBilling B ON A.CorporationId = B.CorporationId
WHERE B.IsActive = 1
于 2013-04-05T15:19:32.217 回答