SQL syntax to select latest instance of a date field (using MYSQL on Linux)

Posted on 2014-03-22
Last Modified: 2014-03-22

I would like to request assistance with an sql statement wherein I would like to update an SSL certificates table with the latest instance of the expiration date of a certificate, in a situation where there are multiple versions of the same certificate installed in a particular network device.  A sample of my existing SQL table looks like this


PRI_KEY   CN_Name     Expiry             Issuer               Device             LatestCNExpiry
1    12-March-2013       Device-a         null
2    13-March-2013       Device-b         null
3    14-March-2015       Device-a         null
4    15-March-2009       Device-a         null
5    23-March-2013       Device-d         null

Open in new window

In this example, "Device-a" has 3 instances of issued by,one that expired in 2009, then 2013, then 2015.  So I want the column LatestCNExpiry to show that the latest version of this cert is 15-March-2015.  Expiry and LatestCNEpxiry would be normal mysql DATA fields, so i can do date computations on them.   The rest are just strings.  In my case, a distinct record (Unique index) would be CN_Name +issuer + Device.   And within the unique record of CN_Name+issuer+device there could be be several different "Expiry" records. The output I am expecting would look like this:

PRI_KEY    CN_Name     Expiry             Issuer               Device                 LatestCNExpiry
1    12-March-2013       Device-a           14-March-2015
2    13-March-2013       Device-b            13-March-2013
3    14-March-2015       Device-a            14-March-2015
4    15-March-2009       Device-a            14-March-2015
5    23-March-2013       Device-d            23-March-2013

Open in new window

So I would want to convert the following "English"  statement to an "SQL" statement but not sure how to set it up:    UPDATE SSL_CERT_PER_DEVICE SET LatestCNExpiry= maximum (Expiry) where CN_NAME + Issuer + device = current record's CN_NAME + Issuer + Device

Thanks very much.
Question by:rleyba828

Assisted Solution

jayakrishnabh earned 40 total points
ID: 39947431
   SELECT CN_Name, Issuer, Device ,Expiry, LatestCNExpiry,
       RN1 = max(Expiry) OVER(PARTITION BY CN_Name, Issuer, Device )      
update CTE
set LatestCNExpiry=RN1
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 40 total points
ID: 39947687
The 'normal' MySQL date format would be 2013-03-12, not 12-March-2013.  You can use 12-March-2013 as a display format but I have never seen it work in MySQL Date functions or comparisons.
LVL 48

Accepted Solution

PortletPaul earned 420 total points
ID: 39948172
Regarding suggested use of: MAX(...) OVER(PARTITION BY ... ORDER BY ...)
I don't believe any MySQL version provides this (or any) analytic function as at March 2014

Try this:
    	(`PRI_KEY` int, `CN_Name` varchar(9), `Expiry` datetime, `Issuer` varchar(10), `Device` varchar(8), `LatestCNExpiry` datetime)
    	(`PRI_KEY`, `CN_Name`, `Expiry`, `Issuer`, `Device`, `LatestCNExpiry`)
    	(1, '', '2013-03-12 00:00:00', '', 'Device-a', '2015-03-14 00:00:00'),
    	(2, '', '2013-03-13 00:00:00', '', 'Device-b', '2013-03-13 00:00:00'),
    	(3, '', '2015-03-14 00:00:00', '', 'Device-a', '2015-03-14 00:00:00'),
    	(4, '', '2009-03-15 00:00:00', '', 'Device-a', '2015-03-14 00:00:00'),
    	(5, '', '2013-03-23 00:00:00', '', 'Device-d', '2013-03-23 00:00:00')
            SELECT `CN_Name`, `Issuer`, `Device`, max(`expiry`) AS max_expiry
            GROUP BY `CN_Name`, `Issuer`, `Device`
           ) AS exp ON  dev.CN_Name = exp.CN_Name
                    AND dev.Issuer  = exp.Issuer
                    AND dev.Device  = exp.Device
   dev.LatestCNExpiry = exp.max_expiry

**Query 1**:


    | PRI_KEY |   CN_NAME |                       EXPIRY |     ISSUER |   DEVICE |               LATESTCNEXPIRY |
    |       1 | | March, 12 2013 00:00:00+0000 | | Device-a | March, 14 2015 00:00:00+0000 |
    |       2 | | March, 13 2013 00:00:00+0000 | | Device-b | March, 13 2013 00:00:00+0000 |
    |       3 | | March, 14 2015 00:00:00+0000 | | Device-a | March, 14 2015 00:00:00+0000 |
    |       4 | | March, 15 2009 00:00:00+0000 | | Device-a | March, 14 2015 00:00:00+0000 |
    |       5 | | March, 23 2013 00:00:00+0000 | | Device-d | March, 23 2013 00:00:00+0000 |



Open in new window


Author Comment

ID: 39948269
Hi PortletPaul,   thanks very much.  It was your solution that worked.  

For Dave,   I just eyeballed the date format because I composed the question when I was at home and not at the office, but yes, you are correct about the format.

For  jayakrishnabh, your solution did not work for mysql., but thanks for trying to come up with a plan.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Query syntax 10 40
Remove alpha from alphanumeric 4 58
Need BIOS update Linux for MSI X99A motherboard. 4 26
linux redhat 7.2 10 36
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL ( several years ago, it seemed like now was a good time to updat…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now