?
Solved

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

Posted on 2014-12-09
4
Medium Priority
?
163 Views
Last Modified: 2014-12-09
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
Comment
Question by:mikegrad7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40489366
>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
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 40489367
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
 
LVL 57

Expert Comment

by:HainKurt
ID: 40489421
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
 
LVL 1

Author Closing Comment

by:mikegrad7
ID: 40489475
great, thanks for the quick feedback!
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question