문제

I am faced with getting the latest (newest) record for a part, and my only way is to do it using a "max date" approach.

Here is a 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 I have above is an ldDate with no time, and an ldTime but not a proper HH:MM:SS representation, it is seconds since midnight, so the first line would be:

2010-10-26 00:00:00.000 52867 or if I convert the time:

52 867 seconds = 14.6852778 hours ~2010-10-26 14:68:52

Is there a clean way I can combine both columns in my SQL query? I want to do a simple max(ldCombinedDate) and group by ldPart (part number).

The id, is almost useless, so ignore it, it is there right now for what order a record was inserted, nothing else, could be used for a nested query maybe? NOTE: Order of entry does not mean latest date...

Thanks...

도움이 되었습니까?

해결책

The expression

DATEADD(second, ldTime, ldDate)

will return the combined date. So, I guess you want something like this:

SELECT ldPart, MAX(DATEADD(second, ldTime, ldDate)) AS MaxCombinedDate
  FROM yourtable
 GROUP BY ldPart
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top