代码之家  ›  专栏  ›  技术社区  ›  ErocM

无法使视图在SQL Server上工作

  •  0
  • ErocM  · 技术社区  · 11 年前

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

    它看起来是这样的:

    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表以及其他两个表中的账号和branth联系起来。

    我是sql新手,所以请友善一点。

    谢谢

    2 回复  |  直到 11 年前
        1
  •  2
  •   paparazzo    11 年前

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

    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
    
        2
  •  0
  •   Hogan    11 年前

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

    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