Skip to content
Advertisement

SQL how to select only single row using joining?

I have the tables:

tblClients:

tblKeys:

tblEntries:

lClientID and lKeyID (optional) refer to above tables.

I need a SQL query which gives me the list of the users from the tblEntries only with maximum datDate field. Now i used the follwing SQL query:

The problem i encountered is the datLastAccess can contains the equal values so i get the duplicates with the two different keys. I need only the single entry per lClientID. If there are two items with the same values of datDate and lClientID but with the different lKeyID i need only the last one.

Now i get:

I don’t need the first row (with empty sValue)

Thanks in advance.

Advertisement

Answer

In MS Access, I would recommend:

Notes:

  • You don’t need tblClients for this query.
  • The where clause is getting exactly one matching row for each entry per client, because it is comparing on the id column.
  • There is no requirement that ids increase with the date — although if that is the case, the query can be further simplified.

In addition, the subquery can take advantage of an index on tblEnties(lclientId, datdate desc, id desc).

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement