最好的方法来处理SQL许多用于查找的连接

时间:2017-11-01 18:17:20

标签: sql sql-server tsql

我想知道是否有更好的方法来处理所有这些连接?我需要多次从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

1 个答案:

答案 0 :(得分:0)

这些连接中的每一个都是NCR中的不同列。我在您发布的查询中看到的唯一真正问题是它是一面文字。将每列移动到查询中的新行使得眼睛更容易维护,更容易维护。我可能会使用比编号更好的别名。它们都有特定的目的,因此使别名相关使得理解查询变得容易多了。否则,您必须不断引用别名列表以了解ViewContactsLookup_5是调查员。 - 肖恩·朗格

e.g:

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
;