Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2012-01-05 : 16:51:34
|
| I have a table with multiple records for a user, 1 of the columns is a date.I wanted to display a report that only brough back the latest entry.TableusernameuseriduserdateDatatestguy,12321,10/11/2012 5:13testguy,12323241,10/1/2012 5:11testguy,1234421,10/15/2012 5:10testguy1,1234421,10/15/2012 5:10I would like for the response to betestguy,1234421,10/15/2012 5:10testguy1,1234421,10/15/2012 5:10I just cant figure this one out, thanks guys! |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-01-05 : 17:22:21
|
| [code]select * from( select *, row_number() over (partition by username order by userdate desc) as RN from YourTable) swhere RN = 1;[/code] |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2012-01-05 : 18:00:35
|
Here is the actual query i am doing now.select l.logcomputerid,lc.computername,l.loguserid,u.username,l.logtimeform log linner join log computer lc on l.logcomputerid = lc.logcomputeridinner join users u on l.loguserid = u.userid*NOTEThis query gets me the unique correct computer data, but now i dont know how i add all the joins to itselect * from( select *, row_number() over (partition by username order by userdate desc) as RN from YourTable) swhere RN = 1;*NOTEHere is what i was able to figure out, but cant seem to get it work, it says ambiguous column nam e'logcomputerid'select * from(select l.logcomputerid,lc.computername,l.loguserid,u.username,l.logtime,row_number() over (partition by logcomputerid order by logtime desc) as RNform log linner join log computer lc on l.logcomputerid = lc.logcomputeridinner join users u on l.loguserid = u.userid) swhere RN = 1;The problem is, it brings back all records for each record in the log table, i wanted it to bring back the lastest date for each computer.So here is what is returned now. The query should just bring back the high lighted columns |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-01-05 : 20:09:28
|
Can you post the exact query you are using? The code you posted does not parse. May be this? (I am guessing)SELECT *FROM ( SELECT l.logcomputerid, l.computername, l.loguserid, u.username, l.logtime, ROW_NUMBER() OVER(PARTITION BY l.logcomputerid ORDER BY logtime DESC) AS RN FROM [log] l INNER JOIN users u ON l.loguserid = u.userid) sWHERE RN = 1; |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2012-01-06 : 10:51:23
|
| How do i get it so it wont show the column 'RN' in my kick out? |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2012-01-06 : 12:17:20
|
| hummm everytime i try to get the RN not to display on the kick out i get errors? |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-01-06 : 12:20:43
|
| In the SELECT list do not include the RN column. Unless there is some other error happening. BTW, what's a kick out? |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|
|
|
|
|