无法获取查看以在SQL Server上工作

时间:2013-04-05 15:11:58

标签: tsql sql-server-2008-r2

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

这就是它的样子:

enter image description here

这是我的疑问:

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的新手,所以要善待。

谢谢!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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