2013-01-18 75 views
1

我是SQL Server的新手,我試圖創建一個包含我所有客戶信息的視圖。SQL Server視圖沒有得到任何結果

Customer是我的主表,如果客戶存在,我想在其他所有表格中列出。

我試過使用SQL Server Management Studio,但它給了我零結果。

這是我的表像,我選擇了:

enter image description here

我想和其他表中分支出來的CorporationAccountNumberAccount和所有列的其餘部分。

這是管理工作室給我:

SELECT  
    Customer.Customer.*, Customer.Miscellaneous2.*, Common.Address.*, 
    Company.CorporationStructure.Branch AS Expr1, Customer.Delivery.*, 
    Customer.Miscellaneous.*, Customer.Account.AccountNumber AS Expr2 
FROM   
    Customer.Account 
INNER JOIN 
    Common.Address ON Customer.Account.AccountId = Common.Address.ForeignId 
INNER JOIN 
    Company.CorporationStructure ON Customer.Account.CorporationStructureId = Company.CorporationStructure.CorporationStructureId 
         AND Customer.Account.CorporationStructureId = Company.CorporationStructure.CorporationStructureId 
INNER JOIN 
    Customer.Customer ON Company.CorporationStructure.Branch = Customer.Customer.Branch 
INNER JOIN 
    Customer.Delivery ON Company.CorporationStructure.Branch = Customer.Delivery.Branch 
INNER JOIN 
    Customer.Miscellaneous ON Company.CorporationStructure.Branch = Customer.Miscellaneous.Branch 
INNER JOIN 
    Customer.Miscellaneous2 ON Company.CorporationStructure.Branch = Customer.Miscellaneous2.Branch 

它返回結果爲零。

有人看到我在做什麼錯了嗎?

=== UPDATE ===

我更新了查詢的建議:

SELECT  Customer.Customer.IsActive, Customer.Customer.CompanyCode, Customer.Customer.Branch, Customer.Customer.Account, Customer.Customer.TypeCompanyTank, 
         Customer.Customer.SalesManCode, Customer.Customer.Name, Customer.Customer.Street, Customer.Customer.City, Customer.Customer.State, 
         Customer.Customer.ZipCode, Customer.Customer.ZipCodeExtension, Customer.Customer.AreaCode, Customer.Customer.PhoneNumber, Customer.Customer.TaxCode, 
         Customer.Customer.CreditCode, Customer.Customer.Type1, Customer.Customer.FinanceChargeCode, Customer.Customer.StatementInvoiceCode, 
         Customer.Customer.OpenItemBalanceForward, Customer.Customer.BillingTransaction, Customer.Customer.BudgetChange, Customer.Customer.DelinquentLetter, 
         Customer.Customer.PriceClassA, Customer.Customer.PriceClassB, Customer.Customer.PriceClassC, Customer.Customer.PriceClassR, 
         Customer.Customer.InvoiceTerms, Customer.Customer.DefaultProductCode, Customer.Customer.DefaultUnitOfIssue, Customer.Customer.Type2, 
         Customer.Customer.BillingCycle, Customer.Customer.CurrentBalance, Customer.Customer.Over30, Customer.Customer.Over60, Customer.Customer.Over90, 
         Customer.Customer.Over120, Customer.Customer.PreviousStatmentBalance, Customer.Customer.DepositAmount, Customer.Customer.LastPaymenRecievedDate, 
         Customer.Customer.LastPaymentRecievedAmount, Customer.Customer.BudgetRate, Customer.Customer.NonBudgetBalance, Customer.Customer.BudgetBalance, 
         Customer.Customer.UnpaidFinanceCharges, Customer.Customer.StartDate, Customer.Customer.CreditRecord, Customer.Customer.CreditLimit, 
         Customer.Miscellaneous2.IsActive AS Expr3, Customer.Miscellaneous2.CompanyCode AS Expr4, Customer.Miscellaneous2.Branch AS Expr5, 
         Customer.Miscellaneous2.Account AS Expr6, Customer.Miscellaneous2.BillingCycleCode, Customer.Miscellaneous2.LatitudeDegree, 
         Customer.Miscellaneous2.LatitudeDecimal, Customer.Miscellaneous2.North, Customer.Miscellaneous2.LongitudeDegree, 
         Customer.Miscellaneous2.LongitudeDecimal, Customer.Miscellaneous2.West, Customer.Miscellaneous2.GEOResultCode, Customer.Miscellaneous2.DeliveryState, 
         Customer.Miscellaneous2.DeliveryZipCode, Customer.Miscellaneous2.DeliveryZipCodeExtension, Customer.Miscellaneous2.RouteBook, 
         Customer.Miscellaneous2.DriverRouteSequence, Customer.Miscellaneous2.StandingPurchaseOrderNumber, Customer.Miscellaneous2.PhoneType2, 
         Customer.Miscellaneous2.AreaCode2, Customer.Miscellaneous2.PhoneNumber2, Customer.Miscellaneous2.PhoneType3, Customer.Miscellaneous2.PhoneNumber3, 
         Customer.Miscellaneous2.EmailAddress, Customer.Miscellaneous2.ElectronicLetterOption, Customer.Miscellaneous2.ElectronicPostcardOption, 
         Customer.Miscellaneous2.ElectronicStatementOption, Customer.Miscellaneous2.ElectronicInvoiceOption, Customer.Miscellaneous2.LossReason, 
         Customer.Miscellaneous2.LossYear, Customer.Miscellaneous2.LossMonth, Customer.Miscellaneous2.GainReason, Customer.Miscellaneous2.GainYear, 
         Customer.Miscellaneous2.GainMonth, Customer.Miscellaneous2.LastSalesActivityDate, Customer.Miscellaneous2.ElectricTankMonitorNumber, 
         Common.Address.AddressId, Common.Address.ForeignId, Common.Address.AddressType, Common.Address.Street AS Expr7, Common.Address.City AS Expr8, 
         Common.Address.State AS Expr9, Common.Address.ZipCode AS Expr10, Common.Address.ZipCodeExtension AS Expr11, Common.Address.FromFlatFile, 
         Common.Address.IsUpdated, Common.Address.IsAdded, Common.Address.ClearUpdateAndAdded, Common.Address.CorporationId, 
         Company.CorporationStructure.Branch AS Expr1, Customer.Delivery.CompanyCode AS Expr12, Customer.Delivery.Branch AS Expr13, 
         Customer.Delivery.Account AS Expr14, Customer.Delivery.IsActive AS Expr15, Customer.Delivery.DegreeDayBase, Customer.Delivery.DeliveryCode, 
         Customer.Delivery.UseCode, Customer.Delivery.PostcardInOutFlag, Customer.Delivery.Driver, Customer.Delivery.Route, Customer.Delivery.RouteSequence, 
         Customer.Delivery.PreviousPercent, Customer.Delivery.PercentFull, Customer.Delivery.TankSerialNumber, Customer.Delivery.TankType, Customer.Delivery.TankUnit, 
         Customer.Delivery.TicketInOutFlag, Customer.Delivery.LockUseOrRateCode, Customer.Delivery.QuantityLastDelivered, Customer.Delivery.TankSize, 
         Customer.Delivery.NumberOfTanks, Customer.Delivery.NumberOfCompanyTanks, Customer.Delivery.GallonsPerDegreeDay, Customer.Delivery.GallonsPerDay, 
         Customer.Delivery.HeatingConfidence, Customer.Delivery.DailyConfidence, Customer.Delivery.LastDeliveryDate, Customer.Delivery.LastDeliveryDegreeDay, 
         Customer.Delivery.OptimumDeliveryDateOrDegreeDay, Customer.Delivery.RunOutDeliveryDateOrDegreeDay, Customer.Delivery.GasCheck, 
         Customer.Delivery.YearToDateDeliveries, Customer.Delivery.YearToDateGasDelivered, Customer.Delivery.TotalGasUsedLastYear, Customer.Delivery.AXGasUsed1, 
         Customer.Delivery.AXGasUsed2, Customer.Delivery.AXGasUsed3, Customer.Delivery.AXGasUsed4, Customer.Delivery.AXGasUsed5, 
         Customer.Delivery.AXDailyUsed1, Customer.Delivery.AXDailyUsed2, Customer.Delivery.AXDailyUsed3, Customer.Delivery.AXDailyUsed4, 
         Customer.Delivery.AXDailyUsed5, Customer.Delivery.AXDays1, Customer.Delivery.AXDays2, Customer.Delivery.AXDays3, Customer.Delivery.AXDays4, 
         Customer.Delivery.AXDays5, Customer.Miscellaneous.IsActive AS Expr16, Customer.Miscellaneous.CompanyCode AS Expr17, 
         Customer.Miscellaneous.Branch AS Expr18, Customer.Miscellaneous.Account AS Expr19, Customer.Miscellaneous.CareOf, Customer.Miscellaneous.DeliveryStreet, 
         Customer.Miscellaneous.DeliveryCity, Customer.Miscellaneous.PurchaseOrderNumber, Customer.Miscellaneous.MasterBillingBranch, 
         Customer.Miscellaneous.MasterBillingAccount, Customer.Account.AccountNumber AS Expr2 
FROM   Customer.Account LEFT OUTER JOIN 
         Common.Address ON Customer.Account.AccountId = Common.Address.ForeignId LEFT OUTER JOIN 
         Company.CorporationStructure ON Customer.Account.CorporationStructureId = Company.CorporationStructure.CorporationStructureId AND 
         Customer.Account.CorporationStructureId = Company.CorporationStructure.CorporationStructureId LEFT OUTER JOIN 
         Customer.Customer ON Company.CorporationStructure.Branch = Customer.Customer.Branch LEFT OUTER JOIN 
         Customer.Delivery ON Company.CorporationStructure.Branch = Customer.Delivery.Branch LEFT OUTER JOIN 
         Customer.Miscellaneous ON Company.CorporationStructure.Branch = Customer.Miscellaneous.Branch LEFT OUTER JOIN 
         Customer.Miscellaneous2 ON Company.CorporationStructure.Branch = Customer.Miscellaneous2.Branch 

現在我得到一個結果,但現在我一遍又一遍的得到同樣的客戶:

enter image description here

+5

要測試此操作,請一次加入一個連接以查看數據丟失的位置。 –

+1

在該語句中有很多INNER JOIN,可能需要對至少某些表使用LEFT JOIN – twoleggedhorse

+0

我已按照您的建議更新了查詢。 – ErocM

回答

1

的原因可能是因爲雙加盟公司的企業結構。這個問題行是:

INNER JOIN 
    Customer.Customer ON Company.CorporationStructure.Branch = Customer.Customer.Branch 
INNER JOIN 
    Customer.Delivery ON Company.CorporationStructure.Branch = Customer.Delivery.Branch 

你需要有CorporateStructure的from子句中出現兩次,一次爲客戶和一次交付。

+0

你是對的,我必須改變查詢,讓它鏈接公司結構,但我需要從帳戶開始。我會做出改變。 – ErocM

相關問題