sql - Date & Time in seperate columns, time in second format, how to get 'max datetime'? -
i faced getting latest (newest) record part, , way using "max date" approach.
here basic schema (and sample data):
lddate ldtime ldpart id 2010-10-26 00:00:00.000 52867 90-r6600-4100 186 2010-11-01 00:00:00.000 24634 90-r6600-4100 187 2010-11-24 00:00:00.000 58785 90-r6600-4100 194 2010-11-24 00:00:00.000 58771 90-r6600-4100 195 2010-11-17 00:00:00.000 29588 90-r6600-4100 201 2010-11-08 00:00:00.000 29196 90-r6600-4100 282 2010-11-08 00:00:00.000 29640 90-r6600-4100 290 2010-10-19 00:00:00.000 58695 90-r6600-4100 350 2010-09-22 00:00:00.000 32742 bh4354-f0 338 2010-09-22 00:00:00.000 32504 bh4354-f0 340 2010-11-17 00:00:00.000 31157 bh4354-f0 206 2010-11-08 00:00:00.000 27601 bh4354-f0 218 2010-11-08 00:00:00.000 27865 bh4354-f0 21 2010-09-22 00:00:00.000 23264 br6950-f0 70 2010-09-22 00:00:00.000 23270 br6950-f0 77 2010-09-24 00:00:00.000 27781 br6950-f0 97 2010-11-24 00:00:00.000 57735 br6950-f0 196
what have above lddate no time, , ldtime not proper hh:mm:ss representation, seconds since midnight, first line be:
2010-10-26 00:00:00.000 52867
or if convert time:
52 867 seconds = 14.6852778 hours ~2010-10-26 14:68:52
is there clean way can combine both columns in sql query? want simple max(ldcombineddate) , group ldpart (part number).
the id, useless, ignore it, there right order record inserted, nothing else, used nested query maybe? note: order of entry not mean latest date...
thanks...
the expression
dateadd(second, ldtime, lddate)
will return combined date. so, guess want this:
select ldpart, max(dateadd(second, ldtime, lddate)) maxcombineddate yourtable group ldpart
Comments
Post a Comment