簡體   English   中英

處理用於查找的 SQL 大量連接的最佳方法

[英]Best Way To Handle SQL Numerous Joins For Lookup

我想知道是否有更好的方法來處理所有這些連接? 我需要多次從 ViewContactsLookup 表中查找聯系人姓名。 有沒有辦法不需要這么多連接?

SELECT        ISNULL(dbo.NCR.Id, '') AS Id, ISNULL(dbo.NCR.NCR, '') AS Ncr, ISNULL(dbo.NCR.AcctgDate, CONVERT(DATETIME, '1900-01-01 00:00:00', 102)) AS AcctgDate, ISNULL(dbo.NCR.CorrectActDate, CONVERT(DATETIME, 
                         '1900-01-01 00:00:00', 102)) AS CorrectActDate, ISNULL(dbo.NCR.AssignedToMORDate, CONVERT(DATETIME, '1900-01-01 00:00:00', 102)) AS AssignedToMorDate, ISNULL(dbo.NCR.CloseoutDate, CONVERT(DATETIME, 
                         '1900-01-01 00:00:00', 102)) AS CloseoutDate, ISNULL(dbo.NCR.CustomerContactYN, '') AS CustomerContactYN, ISNULL(dbo.NCR.ReportedDate, CONVERT(DATETIME, '1900-01-01 00:00:00', 102)) AS ReportedDate, 
                         ISNULL(dbo.NCR.NonConformityComments, '') AS NonConformityComments, ISNULL(dbo.NCR.ProbableCauseComments, '') AS ProbableCauseComments, ISNULL(dbo.NCR.CorrectiveActionComments, '') 
                         AS CorrectiveActionComments, ISNULL(dbo.NCR.Comments, N'') AS Comments, ISNULL(dbo.NCR.InvestigatorId, '') AS InvestigatorId, ISNULL(dbo.NCR.MORId, '') AS MorId, ISNULL(dbo.NCR.CategoryId, '') AS CategoryId, 
                         ISNULL(dbo.NCR.PropertyId, '') AS PropertyId, ISNULL(dbo.NCR.CustomerId, '') AS CustomerId, ISNULL(dbo.NCR.LinkedInfo, '') AS LinkedInfo, ISNULL(dbo.NCR.CreatedById, '') AS CreatedById, ISNULL(dbo.NCR.CreatedDate, 
                         CONVERT(DATETIME, '1900-01-01 00:00:00', 102)) AS CreatedDate, ISNULL(dbo.NCR.LastModifiedById, '') AS LastModifiedById, ISNULL(dbo.NCR.LastModifiedDate, CONVERT(DATETIME, '1900-01-01 00:00:00', 102)) 
                         AS LastModifiedDate, ISNULL(dbo.NCR.OwnerId, '') AS OwnerId, ISNULL(dbo.NCR.InvestDate, CONVERT(DATETIME, '1900-01-01 00:00:00', 102)) AS InvestDate, ISNULL(dbo.NCR.CloseoutYN, '') AS CloseoutYN, 
                         ISNULL(dbo.NCR.ImplementedYN, '') AS ImplementedYN, ISNULL(dbo.NCR.EffectiveYN, '') AS EffectiveYN, ISNULL(dbo.NCR.ActiveYN, '') AS ActiveYN, CASE WHEN ISNULL(LastModifiedDate, '1/1/1900') 
                         <> '1/1/1900' THEN LastModifiedDate ELSE isnull(CreatedDate, '1/1/1900') END AS LastActivityDate, ISNULL(dbo.NCR.AccountId, '') AS AccountId, ISNULL(dbo.NCR.CategoryTxt, '') AS CategoryTxt, ISNULL(dbo.NCR.PropertyTxt, 
                         '') AS PropertyTxt, ISNULL(dbo.NCR.PreventativeActionComments, '') AS PreventativeActionComments, ISNULL(dbo.NCR.AcctgOutlookIds, '') AS AcctgOutlookIds, ISNULL(dbo.NCR.CorrectActOutlookIds, '') AS CorrectActOutlookIds, 
                         ISNULL(dbo.NCR.InvestOutlookIds, '') AS InvestOutlookIds, ISNULL(dbo.NCR.AssignedToMOROutlookIds, '') AS AssignedToMorOutlookIds, ISNULL(dbo.NCR.CategoryICP1Txt, '') AS CategoryIcp1Txt, 
                         ISNULL(dbo.NCR.CategoryICP2Txt, '') AS CategoryIcp2Txt, ISNULL(dbo.NCR.CorrectActMtgTxt, '') AS CorrectActMtgTxt, ISNULL(dbo.NCR.ProductsTxt, '') AS ProductsTxt, ISNULL(dbo.NCR.CorrectActId, '') AS CorrectActId, 
                         ISNULL(dbo.NCR.AssignedToMORId, '') AS AssignedToMorId, ISNULL(dbo.NCR.AccountingComments, '') AS AccountingComments, ISNULL(dbo.NCR.AccountingContactId, '') AS AccountingContactId, 
                         ISNULL(dbo.NCR.AssignedToAccountingId, '') AS AssignedToAccountingId, 
                         CASE WHEN CorrectiveActionComments > '' THEN '<i class="fa fa-check-square-o fa-fw" style="color:green"></i> Completed' ELSE '<i class="fa fa-square-o fa-fw"></i> Incomplete' END AS zzm, 
                         CASE WHEN AccountingComments > '' THEN '<i class="fa fa-check-square-o fa-fw" style="color:green"></i> Completed' ELSE '<i class="fa fa-square-o fa-fw"></i> Incomplete' END AS zza, ISNULL(dbo.NCR.LotNum, '') 
                         AS LotNum, ISNULL(dbo.NCR.ComplaintDescription, '') AS ComplaintDescription, ISNULL(dbo.NCR.PackageConditionTxt, '') AS PackageConditionTxt, ISNULL(dbo.NCR.MaterialDisposalTxt, '') AS MaterialDisposalTxt, 
                         ISNULL(dbo.NCR.MaterialTransferTxt, '') AS MaterialTransferTxt, ISNULL(dbo.NCR.ReturnInspectedById, '') AS ReturnInspectedById, ISNULL(dbo.NCR.ReturnInspectionDate, CONVERT(DATETIME, '1900-01-01 00:00:00', 102)) 
                         AS ReturnInspectionDate, ISNULL(dbo.NCR.ReturnStatusTxt, '') AS ReturnStatusTxt, ISNULL(dbo.NCR.ReturnNewLotNum, '') AS ReturnNewLotNum, ISNULL(dbo.NCR.RGA, '') AS RGA, ISNULL(dbo.NCR.QcStatusTxt, '') 
                         AS QcStatusTxt, ISNULL(dbo.NCR.QcConformingTxt, '') AS QcConformingTxt, ISNULL(dbo.NCR.QcNonconformingTxt, '') AS QcNonconformingTxt, ISNULL(dbo.NCR.QcSupervisorId, '') AS QcSupervisorId, 
                         ISNULL(dbo.NCR.QcSupervisorDate, CONVERT(DATETIME, '1900-01-01 00:00:00', 102)) AS QcSupervisorDate, ISNULL(dbo.NCR.CreditStatusTxt, '') AS CreditStatusTxt, ISNULL(dbo.NCR.CreditDate, CONVERT(DATETIME, 
                         '1900-01-01 00:00:00', 102)) AS CreditDate, ISNULL(dbo.NCR.CreditRespondDate, CONVERT(DATETIME, '1900-01-01 00:00:00', 102)) AS CreditRespondDate, ISNULL(dbo.NCR.CreditAmountDec, '0') AS CreditAmountDec, 
                         ISNULL(dbo.NCR.CreditCommentsTxt, '') AS CreditCommentsTxt, ISNULL(dbo.NCR.CreditById, '') AS CreditById, ISNULL(dbo.NCR.ApprovalSentDate, CONVERT(DATETIME, '1900-01-01 00:00:00', 102)) AS ApprovalSentDate, 
                         ISNULL(dbo.NCR.OperationsApprovedDate, CONVERT(DATETIME, '1900-01-01 00:00:00', 102)) AS OperationsApprovedDate, ISNULL(dbo.NCR.OperationsApprovedById, '') AS OperationsApprovedById, 
                         ISNULL(dbo.NCR.OperationsApprovedCommentsTxt, '') AS OperationsApprovedCommentsTxt, ISNULL(dbo.NCR.SalesApprovedDate, CONVERT(DATETIME, '1900-01-01 00:00:00', 102)) AS SalesApprovedDate, 
                         ISNULL(dbo.NCR.SalesApprovedCommentsTxt, '') AS SalesApprovedCommentsTxt, ISNULL(dbo.NCR.SalesApprovedById, '') AS SalesApprovedById, ISNULL(dbo.NCR.NCRStep, '') AS NCRStep, 
                         ISNULL(ViewContactsLookup_2.FullName, '') AS CorrectActByTxt, ISNULL(ViewContactsLookup_1.FullName, '') AS AssignedToMORByTxt, ISNULL(ViewContactsLookup_3.FullName, '') AS AssignedToAccountingByTxt, 
                         ISNULL(ViewContactsLookup_4.FullName, '') AS AccountingContactTxt, ISNULL(ViewContactsLookup_5.FullName, '') AS InvestigatorTxt, ISNULL(ViewContactsLookup_6.FullName, '') AS MorTxt, 
                         ISNULL(dbo.ViewAccountsLookup.Name, '') AS AccountTxt, ISNULL(ViewContactsLookup_8.FullName, '') AS CreatedByTxt, ISNULL(ViewContactsLookup_7.FullName, '') AS LastModifiedByTxt, 
                         ISNULL(ViewContactsLookup_9.FullName, '') AS OwnerTxt, ISNULL(ViewContactsLookup_10.FullName, '') AS ReturnInspectedByTxt, ISNULL(ViewContactsLookup_11.FullName, '') AS QcSupervisorTxt, 
                         ISNULL(dbo.ViewContactsLookup.FullName, '') AS CreditByTxt, ISNULL(ViewContactsLookup_12.FullName, '') AS OperationsApprovedByTxt, ISNULL(ViewContactsLookup_13.FullName, '') AS SalesApprovedByTxt, 
                         ISNULL(ViewContactsLookup_9.FullName, '') + ' ' + ISNULL(ViewContactsLookup_4.FullName, '') + ' ' + ISNULL(ViewContactsLookup_6.FullName, '') AS AssignedToTxt
FROM            dbo.NCR LEFT OUTER JOIN
                         dbo.ViewContactsLookup ON dbo.NCR.CreditById = dbo.ViewContactsLookup.Id LEFT OUTER JOIN
                         dbo.ViewContactsLookup AS ViewContactsLookup_13 ON dbo.NCR.SalesApprovedById = ViewContactsLookup_13.Id LEFT OUTER JOIN
                         dbo.ViewContactsLookup AS ViewContactsLookup_12 ON dbo.NCR.OperationsApprovedById = ViewContactsLookup_12.Id LEFT OUTER JOIN
                         dbo.ViewContactsLookup AS ViewContactsLookup_11 ON dbo.NCR.QcSupervisorId = ViewContactsLookup_11.Id LEFT OUTER JOIN
                         dbo.ViewContactsLookup AS ViewContactsLookup_10 ON dbo.NCR.ReturnInspectedById = ViewContactsLookup_10.Id LEFT OUTER JOIN
                         dbo.ViewContactsLookup AS ViewContactsLookup_9 ON dbo.NCR.OwnerId = ViewContactsLookup_9.Id LEFT OUTER JOIN
                         dbo.ViewContactsLookup AS ViewContactsLookup_7 ON dbo.NCR.LastModifiedById = ViewContactsLookup_7.Id LEFT OUTER JOIN
                         dbo.ViewContactsLookup AS ViewContactsLookup_8 ON dbo.NCR.CreatedById = ViewContactsLookup_8.Id LEFT OUTER JOIN
                         dbo.ViewAccountsLookup ON dbo.NCR.AccountId = dbo.ViewAccountsLookup.Id LEFT OUTER JOIN
                         dbo.ViewContactsLookup AS ViewContactsLookup_6 ON dbo.NCR.MORId = ViewContactsLookup_6.Id LEFT OUTER JOIN
                         dbo.ViewContactsLookup AS ViewContactsLookup_5 ON dbo.NCR.InvestigatorId = ViewContactsLookup_5.Id LEFT OUTER JOIN
                         dbo.ViewContactsLookup AS ViewContactsLookup_4 ON dbo.NCR.AccountingContactId = ViewContactsLookup_4.Id LEFT OUTER JOIN
                         dbo.ViewContactsLookup AS ViewContactsLookup_3 ON dbo.NCR.AssignedToAccountingId = ViewContactsLookup_3.Id LEFT OUTER JOIN
                         dbo.ViewContactsLookup AS ViewContactsLookup_1 ON dbo.NCR.AssignedToMORId = ViewContactsLookup_1.Id LEFT OUTER JOIN
                         dbo.ViewContactsLookup AS ViewContactsLookup_2 ON dbo.NCR.CorrectActId = ViewContactsLookup_2.Id

每個連接都指向 NCR 中的不同列。 我在您發布的查詢中看到的唯一真正問題是它是一堵文字牆。 將查詢中的每一列移動到一個新行,這樣看起來更容易,也更容易維護。 我可能會使用比編號更好的別名。 它們都有特定的用途,因此使別名相關可以更容易地理解查詢。 否則,您必須不斷參考別名列表才能知道 ViewContactsLookup_5 是什么調查員。 — 肖恩·蘭格

例如:

SELECT
      dbo.NCR.id
      , ISNULL(correctact.FullName, '') AS correctactbytxt
      , ISNULL(assignedtomor.FullName, '') AS assignedtomorbytxt
      , ISNULL(assignedtoaccounting.FullName, '') AS assignedtoaccountingbytxt
      , ISNULL(accountingcontact.FullName, '') AS accountingcontacttxt
      , ISNULL(investigator.FullName, '') AS investigatortxt
      , ISNULL(mor.FullName, '') AS mortxt
      , ISNULL(creditby.Name, '') AS accounttxt
      , ISNULL(createdby.FullName, '') AS createdbytxt
      , ISNULL(lastmodifiedby.FullName, '') AS lastmodifiedbytxt
      , ISNULL(owner.FullName, '') AS ownertxt
      , ISNULL(returninspectedby.FullName, '') AS returninspectedbytxt
      , ISNULL(qcsupervisor.FullName, '') AS qcsupervisortxt
      , ISNULL(dbo.ViewContactsLookup.FullName, '') AS creditbytxt
      , ISNULL(operationsapproved.FullName, '') AS operationsapprovedbytxt
      , ISNULL(salesapprovedby.FullName, '') AS salesapprovedbytxt
      , ISNULL(owner.FullName, '') + ' ' + ISNULL(accountingcontact.FullName, '') + ' ' + ISNULL(mor.FullName, '') AS assignedtotxt
FROM dbo.NCR
LEFT OUTER JOIN dbo.ViewAccountsLookup ON dbo.NCR.AccountId = dbo.ViewAccountsLookup.Id
LEFT OUTER JOIN dbo.ViewContactsLookup AS AccountingContact ON dbo.NCR.AccountingContactId = AccountingContact.Id
LEFT OUTER JOIN dbo.ViewContactsLookup AS AssignedToAccounting ON dbo.NCR.AssignedToAccountingId = AssignedToAccounting.Id
LEFT OUTER JOIN dbo.ViewContactsLookup AS AssignedToMOR ON dbo.NCR.AssignedToMORId = AssignedToMOR.Id
LEFT OUTER JOIN dbo.ViewContactsLookup AS CorrectAct ON dbo.NCR.CorrectActId = CorrectAct.Id
LEFT OUTER JOIN dbo.ViewContactsLookup AS CreatedBy ON dbo.NCR.CreatedById = CreatedBy.Id
LEFT OUTER JOIN dbo.ViewContactsLookup AS CreditBy ON dbo.NCR.CreditById = CreditBy.Id
LEFT OUTER JOIN dbo.ViewContactsLookup AS Investigator ON dbo.NCR.InvestigatorId = Investigator.Id
LEFT OUTER JOIN dbo.ViewContactsLookup AS LastModifiedBy ON dbo.NCR.LastModifiedById = LastModifiedBy.Id
LEFT OUTER JOIN dbo.ViewContactsLookup AS MOR ON dbo.NCR.MORId = MOR.Id
LEFT OUTER JOIN dbo.ViewContactsLookup AS OperationsApproved ON dbo.NCR.OperationsApprovedById = OperationsApproved.Id
LEFT OUTER JOIN dbo.ViewContactsLookup AS Owner ON dbo.NCR.OwnerId = Owner.Id
LEFT OUTER JOIN dbo.ViewContactsLookup AS QcSupervisor ON dbo.NCR.QcSupervisorId = QcSupervisor.Id
LEFT OUTER JOIN dbo.ViewContactsLookup AS ReturnInspectedBy ON dbo.NCR.ReturnInspectedById = ReturnInspectedBy.Id
LEFT OUTER JOIN dbo.ViewContactsLookup AS SalesApprovedBy ON dbo.NCR.SalesApprovedById = SalesApprovedBy.Id
;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM