Question: For every AbsenceBalance.AbsenceTypesUID I want to return the latest record AbsenceBalance.BalanceTime for each AbsenceBalance.EmployeeUID

I have tried select max but it only returns the most recent entry for the entire table and not by AbsenceBalance.AbsenceTypesUID or AbsenceBalance.EmployeeUID

This is my query

```
SELECT TOP (1000) 
AbsenceBalance.[UID],
AbsenceBalance.BalanceTime,
AbsenceBalance.AbsenceTypesUID,
AbsenceBalance.Mins,
Employee.FullName,
Employee.FirstName,
Employee.LastName,
AbsenceBalance.EmployeeUID,
absencetypes.LongName

  from [RiteqDB].[dbo].[AbsenceBalance]

  LEFT JOIN [RiteqDB].[dbo].Employee on AbsenceBalance.EmployeeUID = Employee.UID

  LEFT JOIN [RiteqDB].[dbo].AbsenceTypes on absencebalance.AbsenceTypesUID = absencetypes.UID 

  where AbsenceBalance.[UID] = (select max (AbsenceBalance.[UID]) from [RiteqDB].[dbo].[AbsenceBalance] where AbsenceBalance.AbsenceTypesUID = AbsenceBalance.AbsenceTypesUID)

  --where Select Max(v) from (values (AbsenceBalance.BalanceTime)

  order by FullName, AbsenceTypesUID

```

Answer: 
It sounds like you might need a group by [link](https://www.w3schools.com/sql/sql_groupby.asp), then either use an inner select in a where (like you have) or use this with an inner join.

```
SELECT 
Max(AbsenceBalance.[UID]),
AbsenceBalance.AbsenceTypesUID,
AbsenceBalance.EmployeeUID,

from [RiteqDB].[dbo].[AbsenceBalance]

GROUP BY AbsenceTypesUID, EmployeeUID

```
In your where condition your comparing with itself that might be the issue.

```
select max (AbsenceBalance.[UID]) from [RiteqDB].[dbo].[AbsenceBalance] 
where AbsenceBalance.AbsenceTypesUID = AbsenceBalance.AbsenceTypesUID

```

following is wrong

```
AbsenceBalance.AbsenceTypesUID = AbsenceBalance.AbsenceTypesUID

```