Microsoft
Software
Hardware
Network
Question : SQL Query - displaying latest entry per user
I have a query that I want to produce the latest entry(visit) for each of the user. In my head the query goes like this: For each user, show me the most recent visit that they had and all of the information from the visit. Given that there are one to many visits per user in this table. How can I return only the most recent visits without have to do some crazy joins? Is there a more simple way of structuring this query?
Here is the table structure:
Visit {
id int (Primary Key)
visit_date date
user_id int (foreign key)
field1 nvarchar(255)
field2 nvarchar(255)
...
}
To me it looks like a classic case of combining aggregate function with non-aggregate functions of the same table.
Answer : SQL Query - displaying latest entry per user
SELECT *
FROM Visits v
WHERE v.Visit_Date = (SELECT max(Visit_date) from visits v1 where v1.User_ID = v.User_ID )
Random Solutions
Any way to use old printer on Windows 7
Why do I get an access denied error when I try to view the files inside the folder redirect folders?
Slider Control 6 in Access 2003 and Continous Froms
Extract OLE Object from an MS Access database
How to move SQL Data to another drive?
Outlook 2003 Contact List >> File As: Need to mass change format.
capturing transaction rollbacks using SQL Server 2008 Audit
The operation you selected will convert the selected basic disk(s) to dynamic disk(s) on Windows Server 2008 - negative consequences?
Hide Username & Password in FTP URL
How to retrieve error number and or message from osql run from command line