Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

sql - Using Case Statement to compare four columns and adding the earliest date to the new column

I'm trying to have a column that lists the earliest date out of four columns. I've tried this a Case Statement and a tin that should list the s.Open_date, instead, it's placing the 1/1/2020 in the column. Here's the data

tin     S.Open_Date     LoanOpen    CCOpenDate    MortgageDate    EarliestDate
1        5/5/2015        11/19/2018    null        9/25/2017         1/1/2020

So in the example above, it should have 5/5/2015 in the EarliestDate column.

I've placed the whole query below. That said, everything in the query works correctly except the Case Statement. Is there a better way to do it?

select i.mem, min(s.open_date) as ShareOpenDate, min(l.loanopen) as LoanOpen, min(ecc.cc_opendate) as CCopenDate, min(m.MortgageDate) as MortgageDate,

Case
when (s.open_date > l.loanopen and s.open_date > ecc.cc_opendate AND s.open_date > m.MortgageDate) then s.open_date
when (l.loanopen > s.open_date and l.loanopen > ecc.cc_opendate AND l.loanopen > m.MortgageDate) then l.loanopen
when (ecc.cc_opendate > s.open_date and ecc.cc_opendate > l.loanopen AND ecc.cc_opendate > m.MortgageDate) then ecc.cc_opendate
when (m.MortgageDate > s.open_date and m.MortgageDate > l.loanopen AND m.MortgageDate > ecc.cc_opendate) then m.MortgageDate
else '1/1/2020' END as EarliestDate

from individual as i
inner join membershipparticipant as mp on i.individual_id=mp.individual_id and mp.participation_type=101
inner join share as s on mp.member_nbr=s.member_nbr

#Subquery to bring in loan information
left outer join (
select member_nbr, min(open_date) as loanopen
from loan as l
group by member_nbr
)l  on s.member_nbr=l.member_nbr

#Subquery to bring in CC Information
left outer join ( 
select savings_acct_nbr, min(open_date) as cc_opendate
from externalcreditcard
group by savings_acct_nbr
) ecc on s.member_nbr=ecc.savings_acct_nbr

Subquery to bring in mortgage information
left outer join (
select MC_PMT_MBR_NBR as MortgageMember, min(MC_Loan_Date) as MortgageDate
from EXTERNALMORTGAGEMC
group by MC_PMT_MBR_NBR
)m on s.member_nbr=m.MortgageMember

group by i.mem
question from:https://stackoverflow.com/questions/65894425/using-case-statement-to-compare-four-columns-and-adding-the-earliest-date-to-the

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The easiest method is to use a subquery inside the SELECT, although there may be a small performance implication:

SELECT
...
, (SELECT MIN(x)
    FROM (VALUES
      (l.loanopen),
      (s.open_date),
      (ecc.cc_opendate),
      (m.MortgageDate),
      ('1/1/2020')
   ) AS v (x)) AS EarliestDate
FROM ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...