i am trying to retrieve data from tables and my script looks like this->
SELECT
e.*,
c.ClientName
FROM
Entities e WITH(nolock)
JOIN Clients c WITH(nolock) ON c.ClientId = e.ClientId
WHERE
e.ClientId = 222222
part of output is->
entityid clientid version clientname
1 222222 1 first
2 222222 1 second
what i need to get is for this clientid, fetch all entities with their latest version.
what i have written->
SELECT
e.*,
c.ClientName
FROM
Entities e WITH(nolock)
JOIN Clients c WITH(nolock) ON c.ClientId = e.ClientId
WHERE
e.[Version] = (SELECT MAX([Version]) FROM Entities b WHERE e.EntityId=b.EntityId) AND
e.ClientId = 222222
what this gives me->
entityid clientid version clientname
1 222222 1 first
which is wrong. can someone help?