Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

SQL select max time from accessed table for each object in a list

I am trying to write a query which, based on a set of IDs, gives me the latest time for that ID from another table. This is basically showing users who accessed an item. the users are stored in one table, while the access logs are stored in another.

I want to be able to provide a list of ids (where user.id in (xyx) ) and get one row representing the latest time from the accessed table. Can someone help? i have a query right now which shows an extract for all users and accesses within a time range. I think this can be modified but i cant figure it out.

select distinct
AccessLog.ACCESSED as   time_accessed,
AccessLog.ACCESSTYPE     as access_type,
AccessLog.USERID    as user_id,
USERS.Name as USERNAME
from AccessLog 
join USERS on AccessLog.USERID =  USERS.id 
where accessed between  '2014-11-12 11:00:00'  and  '2014-11-25 10:00:00'  
order by accessed                                                                                             
                                                                                           

Open in new window

0
mikegrad7
Asked:
mikegrad7
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>based on a set of IDs, gives me the latest time for that ID from another table.
> the users are stored in one table, while the access logs are stored in another.
Not really following your code block, so I'll just whip this up based on the above statements and let you figure it out.   Subquery to get the max login times for every user, the a main query to reference the id's you want in the return set, LEFT JOIN on the subquery.
SELECT one.id, two.max_date
FROM one_table one
   LEFT JOIN (
      SELECT id, MAX(date_column) as max_date
      FROM another_table 
      GROUP BY id) two ON one.id = two.id
ORDER BY one.id

Open in new window

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you may want to read this article:
http://www.experts-exchange.com/Database/Miscellaneous/A_3203-DISTINCT-vs-GROUP-BY-and-why-does-it-not-work-for-my-query.html
it will explain the "issue" and the path to the working SQL
0
 
HainKurtSr. System AnalystCommented:
try this:

select * from (
 select l.ACCESSED as time_accessed, l.ACCESSTYPE  as access_type, l.USERID as user_id, u.Name as USERNAME,
 row_number() over (partition by u.userid order by l.accessed desc) as rn
 from AccessLog l
 inner join USERS u on l.USERID =  u.id
 where accessed between  '2014-11-12 11:00:00'  and  '2014-11-25 10:00:00'
) x where rn=1

Open in new window

0
 
mikegrad7Author Commented:
great, thanks for the quick feedback!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now