tsql - Correct sql/hql query (aggregate in where clause) -


I want to query the query as below but the question is wrong but describes my intentions.

  select name, date time, data from record, date time = maximum (date time)  

Update: OK The description of the query intentions is not very good. My bad

I want to select the latest record for each person.

i like Mickey's answer and quosnoye (and upyoyed miki), but if your needs are similar to me , You should keep in mind some limitations. First and foremost, this works only when you are looking for the latest record or latest record for the same name. If you want the latest record for each person in a set (a record for each person but the latest record for each), then the above solutions are small. Second, and less importantly, if you work with a large dataset, then it can be slow for a long time. So, what's around the work?

Whatever I do is to add a small field to mark the "latest" in the table. Then, when I store a record (which is done in the stored procedure in SQL Server), then I follow this method:

  update table set newest = 0 where name = Insert in name (name, datetime, data, latest) value (@ name, gatedate), @ data, 1);  

In addition to this, the name is the latest to choose an index and the fastest selection.

Then the selection is simply:

  choose the date of the date, the table from the data where (name = @name) and (newest = 1);  

A selection for a group would be something like this:

  select the name, datetime, data from the table where (newest = 1); - Multiple records are recorded  

If the date of the record can not be entered in order, then your argument is slightly different:

  update table set Latest = 0 where name = @ name in the table (name, date, time, data, latest) value (@ name, gatedate (), @ data, 0); - Note Zero Update Table Set is the latest = 1 where dateTimeVal = (Select Max (dateTimeVal) from table where name = @name);  

The rest remains the same.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

jQuery UI: Datepicker month format -

php - multilevel menu with multilevel array -