| Author |
Topic |
|
matrixr
Starting Member
26 Posts |
Posted - 2006-07-16 : 18:55:33
|
| company table (companyID int, name varchar(50))log table (logID int, companyID int, createdOn datetime)i want to get the top 1 record from the log table for each company ordered by createdon desc, how do i do this without using temp tables?for example:logID | companyID | createdOn11 | 1 | 17/07/0623 | 4 | 17/07/0643 | 5 | 17/07/06 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2006-07-16 : 19:01:05
|
| This assumes that companyID, createdOn is unique in the logtable:select l.logID, l.companyID, l.createdOnfrom logTable ljoin(select companyID, max(createdOn) as maxCreatedOn) aon l.companyID = a.companyID and l.createdOn = a.maxCreatedOnrockmoose |
 |
|
|
matrixr
Starting Member
26 Posts |
Posted - 2006-07-16 : 19:32:09
|
| not unique, this is what i have nowselect l.logID, l.companyID, l.createdOnfrom log l inner join (select companyID, max(createdOn) as maxCreatedOn from log group by companyID) a on a.companyID = l.companyID and a.maxCreatedOn = l.createdOnworks fine except it returns duplicate rowslogID | companyID | createdOn11 | 1 | 17/07/0612 | 1 | 17/07/06...etcany other ideas? |
 |
|
|
matrixr
Starting Member
26 Posts |
Posted - 2006-07-16 : 19:37:25
|
| ... inner join (select max(logID) as logID, companyID, max(createdOn) as maxCreatedOn from Log group by companyID) a on l.logID = a.logID and l.companyID = a.companyID and l.createdOn = a.maxCreatedOn ...worksthanks |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2006-07-16 : 20:06:16
|
| I think you are interested in the last createdOn for each company, the logID in itself is uninteresting.If you join the derived table directly to the Company table I think you get what you want.select c.companyID, c.name, a.maxCreatedOnfrom companyTable cjoin(select companyID, max(createdOn) as maxCreatedOn group by companyID) aon c.companyID = a.companyID???rockmoose |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-07-17 : 12:13:46
|
Or this...--dataset dateformat dmydeclare @log table (logID int, companyID int, createdOn datetime)insert @log select 11, 1, '17/07/06'union all select 12, 1, '17/07/06'union all select 13, 1, '14/07/06'union all select 21, 4, '15/07/06'union all select 22, 4, '16/07/06'union all select 23, 4, '17/07/06'union all select 43, 5, '17/07/06'union all select 44, 5, '18/07/06'--calculationselect * from @log awhere logID in (select top 1 logID from @log where companyID = a.companyID order by createdOn desc, logID desc)/*resultslogID companyID createdOn ----------- ----------- ------------------------------------------------------ 12 1 2006-07-17 00:00:00.00023 4 2006-07-17 00:00:00.00044 5 2006-07-18 00:00:00.000*/ Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2006-07-17 : 13:02:58
|
Or just...select * from @log awhere logID = ( select max(b.logID) from @log b where b.createdOn = ( select max(createdOn) from @log b where a.companyID = b.companyID) and a.companyID = b.companyID) but plain ol' sql is ok...select max(l.logID) as logID, l.companyID, l.createdOnfrom @log linner join (select companyID, max(createdOn) as maxCreatedOn from @log group by companyID) a on a.companyID = l.companyID and a.maxCreatedOn = l.createdOngroup by l.companyID, l.createdOn |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|