多次连系ABC

时间:2018-12-04 14:43:11

标签: sql-server join multiple-columns

  • 我想要不同的数字,例如Credit,Funding,App,ClosesDate 贷款官员。
  • 我有一个表格[E]。[dbo]。[Main],在该表格上应用了不同的过滤器, 然后尝试加入他们。此主表具有GUID作为主键。

请建议我如何加入这些表格。

    Select A.loan_officer, A.OrganizationId, [C].Credit as Credit,      
    [P].Application as 'App', [F].Funding as Fund, [B].ClosingDate as Brok      
    from

    --Credit Pulls     
    (SELECT Credit
    FROM  [E].[dbo].[Main]
    where Loan_Officer is not null
    and LOANFOLDER IN ('Pipeline', 'Prospect', 'Employee Pipeline')) as [C], 

    --Application Units
    (SELECT Application
    FROM  [E].[dbo].[Main]
    where loan_Officer is not null
    and LOANFOLDER IN ('Pipeline', 'Prospect', 'Employee Pipeline')
    and Denial_Date is null ) as [P],

    --Funding Units      
    (SELECT Funding
    FROM  [E].[dbo].[Main]
    where loan_Officer is not null
    and LOANFOLDER IN ('Pipeline', 'Prospect', 'Employee Pipeline')
    and Denial_Date is null) as [F],

    -- Brokered Units 
    (SELECT ClosingDate
    FROM [E].[dbo].[Main]
    where loan_Officer is not null
    and LOANFOLDER IN ('Pipeline', 'Prospect', 'Employee Pipeline')
    and Loan_Info_Channel_F2626 like 'Brok%'
    ) as [B],

    [E].[dbo].[Main] A     
    Join  [B] on B.ClosingDate_748 =A.ClosingDate_748        
    Join F on F.Funding_Date= A.Funding_Date         
    Join P on A.Application_Date= A.Application_Date               
    Join C on C.Credit_Pull_Date = A.Credit_pull_date         

2 个答案:

答案 0 :(得分:3)

我在这里使用scsimon ...不需要联接,只需使用多个case语句即可;当前作为派生表的每一列都有一个。

SELECT Loan_officer
     , OrganizationID
     , CASE WHEN Loan_officer is not null and 
       LOANFOLDER IN ('Pipeline', 'Prospect', 'Employee Pipeline') THEN Credit else NULL end as Credit
     , CASE WHEN Loan_officer is not null and 
       LOANFOLDER IN ('Pipeline', 'Prospect', 'Employee Pipeline') and
       Denial_date is null  THEN Credit else NULL end as Apps
     , CASE WHEN Loan_officer is not null and 
       LOANFOLDER IN ('Pipeline', 'Prospect', 'Employee Pipeline') and
       Denial_date is null  THEN Credit else NULL end as Funding as Fund,
     , CASE WHEN Loan_officer is not null and 
       LOANFOLDER IN ('Pipeline', 'Prospect', 'Employee Pipeline') and
       Loan_Info_Channel_F2626 like 'Brok%' THEN ClosingDate else NULL end as Brok
FROM  [E].[dbo].[Main]

答案 1 :(得分:1)

@xQbert对此进行了艰苦的工作,我将对此稍作扩展,以提取常见的搜索条件,以便您可以进行一些索引编制,而不仅仅是扫描整个表。我还只是清理了case语句中的某些列以返回正确的数据:

SELECT Loan_officer
     , OrganizationID
     ,Credit
     , CASE WHEN Denial_date is null  THEN [Application] ELSE NULL END as App
     , CASE WHEN Denial_date is null  THEN Funding else NULL end as Fund
     , CASE WHEN Loan_Info_Channel_F2626 like 'Brok%' THEN ClosingDate ELSE NULL end as Brok
FROM  [E].[dbo].[Main]
WHERE Loan_officer IS NOT NULL AND LOANFOLDER IN ('Pipeline', 'Prospect', 'Employee Pipeline')