I need to pick the last value for the `group_id` ordered by `id` from the following example table:

```
drop table if exists #temp1
create table #temp1 (group_id int, id int, val varchar(10))
insert into #temp1 values (1111, 1, 'Yes')
insert into #temp1 values (1111, 2, 'No')
insert into #temp1 values (1111, 3, NULL)
insert into #temp1 values (2222, 5, 'No')
insert into #temp1 values (2222, 3, NULL)
insert into #temp1 values (2222, 1, 'No')

```

The expected result is `1111 - Yes` and `2222 - No`.

If I write the following query, it seems to pick the last value based on how the rows are ordered in the table and not by `id` column.

```
SELECT group_id, MAX(last_val)
FROM
(
    SELECT a.group_id, LAST_VALUE(a.val) OVER (PARTITION BY a.group_id ORDER BY a.group_id) AS last_val FROM #temp1 a
) a
GROUP BY group_id

```

If I write the following, it seems to do a `Max` of `val` alphabetically:

```
SELECT group_id, MAX(last_val)
FROM
(
    SELECT a.group_id, LAST_VALUE(a.val) OVER (PARTITION BY a.group_id ORDER BY a.id) AS last_val FROM #temp1 a
) a
GROUP BY group_id

```

In both cases, the results are different from what I need. Can someone please suggest how to get the `val` for the last `id`?