mysql - Select row based on max date across several columns -


for mysql database

i have table includes duplicate rows because of date values in several columns. looking select single row each unique customer id based on max date value evaluated across several date columns

[customer id, startdate, stopdate, modifydate, buydate]

for each customer id, i'd return row has maximum date either in startdate, stopdate, modifydate or buydate columns ( there nulls in date columns.

editing include example - sure how create table here:

*** edit

been trying quite awhile create table here example. can't figure out. posting image? desired rows returned indicated in red.

enter image description here

assuming values never null, can use greatest():

select t.* table t greatest(t.startdate, stopdate, buydate) =           (select max(greatest(t.startdate, stopdate, buydate))            t t2            t2.customerid = t.customerid           ); 

note: return multiple rows customer if more 1 row contains maximum date.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -