3

I have a table that looks like the below in SQL

enter image description here

I would like to select the row with the earliest date in either the PR create date or AC create date field. So in this case, I would want these two records.

enter image description here

I know that I can write

Select email, min(PR_create_date) from table 
group by email

However, this will only find the minimum date in the PR_create_date column. How can I find the minimum date in either the PR_create_date or AC_create_date columns?

3
  • what do you mean by minimum date in either the PR_create_date or AC_create_date
    – saikumarm
    Jan 19, 2016 at 6:25
  • Correct. It has one or the other per row.
    – dsm309
    Jan 19, 2016 at 6:26
  • @saikumarm Basically, I want to know the earliest date that appears for an email address. That date can be either the PR_create_date or the AC_create_date.
    – dsm309
    Jan 19, 2016 at 6:28

5 Answers 5

5

You can use

SELECT email,
       MIN(least(pr_create_date, ac_create_date))
  FROM my_table
 GROUP BY email

Function least returns the minimal value of its arguments.

But if one of arguments is NULL then the result is NULL too. So you are to write some logic to handle nulls, depending on how nulls must be treated in your business logic: for example you can use "special date" in the far past or future to replace nulls as

SELECT email,
       MIN( least( ifnull(pr_create_date, DATE '2999-12-31'),
                   ifnull(ac_create_date, DATE '2999-12-31')) )
  FROM my_table
 GROUP BY email

But in the simplest case when dates are excluding (i.e. in every row exactly one of the dates is null) its enough to just write

SELECT email,
       MIN( ifnull(pr_create_date, ac_create_date) )
  FROM my_table
 GROUP BY email
2
  • Then provide the logic on how to handle null values.
    – Shadow
    Jan 19, 2016 at 6:38
  • This seems to be working. However, it is returning NULL as the earliest date since all records have NULL in one or the other column. How can I exclude NULL values from this? @diziaq I want to get the next highest value from NULL
    – dsm309
    Jan 19, 2016 at 6:43
2

One more solution :

 select temp.EMAIL, min(temp.PR_AC_CREATE_DATE)
 from
 (
  select EMAIL , min(PR_CREATE_DATE) as PR_AC_CREATE_DATE
  from table
  group by email

  union

  select EMAIL, min(AC_CREATE_DATE)
  from table
  group by email
  ) temp

 group by temp.EMAIL ;
0

Use COALESCE function, solution is for generic sql may be run on mysql

select email,min(COALESCE(PR_create_date,AC_create_date)) as mindt 
from tab
group by email
4
  • he is not asking for the first not null value
    – saikumarm
    Jan 19, 2016 at 6:29
  • in comment, he put " It has one or the other per row." Jan 19, 2016 at 6:31
  • Check the required output, it has only 2 records. Your solution would return all records from the table.
    – Shadow
    Jan 19, 2016 at 6:41
  • @SomnathKadam@shadow This seems to be working just as well as using the simple case in the accepted solution. This is equivalent to the below, no? SELECT email, MIN( ifnull(pr_create_date, ac_create_date) ) FROM my_table GROUP BY email
    – dsm309
    Jan 19, 2016 at 18:42
0

You can try a more robust solution like:

SELECT * FROM (SELECT * FROM my_table ORDER BY pr_create_date DESC,ac_create_date DESC LIMIT 5);

Order by: specify columns and ordering rule (ascending ASC(default) / descending DESC)

Limit: number of records you want. This will give the top 5 latest records. Try it.

-1
Select email,
       Case When PR_create_date < AC_create_date Then PR_create_date
            When AC_create_date < PR_create_date Then AC_create_date             
            End As TheMin
From   YourTableNameHere
Group By email 
3
  • If you compare null with a value, then the result will also be null.
    – Shadow
    Jan 19, 2016 at 6:47
  • @Shadow: Yes I am completely agree with you. Can you provide solution in this case? Your help is highly appreciated. Jan 19, 2016 at 6:55
  • There is an accepted answer that takes care of this.
    – Shadow
    Jan 19, 2016 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.