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
Finding value in a Range
Outlook 2007 Corrupt ost file
how to export datatable to Excel using Save dialog in VB.NET
SQL 2005 express. Creating tables from a script
MS Virtual Machine and disk2vhd
"You don't have the license required to use this Active X Control"
How to show number of messages besides a folder
Issue Opening Excel File in MS Excel 2007 w/Embedded Links
Corrupt Excel file - unable to recover from all machines
Missing day no. when using FormatDateTime