hello to all
This is an extension to an earlier post.
Starting with tbl1, I want to get to tbl3 (far right). In tbl3, the 'TheVal' values are listed in ASC order, and the 'ID_RowNum' numbers each row within each ID group.
I have code that does this perfectly, but it is somewhat indirect. (the complication is that THERE ARE REPEATED values of 'TheVal' within some ID groups. I'm hoping for a more direct solution.
Here's how I do it now...
a) From tbl1, I produce Qry2 (using code shown below...)
b) Then, I APPEND the results of Qry2 into a temp table that has an AutoNumber field, AutoID.
c) Then, I use a query very similar to qry2, but ranking each ID's records by the AutoID field, producing the ID_RowNum seen in tbl3.
Is there a more efficient way to go directly from tbl1 to tbl3?
Thanks in advance!
This is an extension to an earlier post.
Starting with tbl1, I want to get to tbl3 (far right). In tbl3, the 'TheVal' values are listed in ASC order, and the 'ID_RowNum' numbers each row within each ID group.
I have code that does this perfectly, but it is somewhat indirect. (the complication is that THERE ARE REPEATED values of 'TheVal' within some ID groups. I'm hoping for a more direct solution.
Code:
[b]Tbl1[/b][u][/u] [b]Qry2[/b][u][/u] [b]Tbl3[/b][u][/u]
[b]ID TheVal[/b] [b]ID TheVal ID_Rank[/b] [b]ID TheVal ID_RowNum[/b]
1 34 1 8 1 1 8 1
1 8 1 12 2 1 12 2
1 12 1 19 3 1 19 3
1 19 1 19 3 1 19 4
1 19 1 21 5 1 21 5
1 21 1 34 6 1 34 6
2 18 2 9 1 2 9 1
2 18 2 18 2 2 18 2
2 77 2 18 2 2 18 3
2 77 2 18 2 2 18 4
2 9 2 77 5 2 77 5
2 18 2 77 5 2 77 6
3 12 3 4 1 3 4 1
3 4 3 12 2 3 12 2
3 19 3 19 3 3 19 3
3 21 3 21 4 3 21 4
3 21 3 21 4 3 21 5
3 34 3 34 6 3 34 6
Here's how I do it now...
a) From tbl1, I produce Qry2 (using code shown below...)
b) Then, I APPEND the results of Qry2 into a temp table that has an AutoNumber field, AutoID.
c) Then, I use a query very similar to qry2, but ranking each ID's records by the AutoID field, producing the ID_RowNum seen in tbl3.
Code:
[b]Qry2[/b]
SELECT
a.ID,
a.TheVal,
(SELECT
COUNT(*)
FROM
q2b_Uni b
WHERE
(a.ID = b.ID) AND
(a.TheVal > b.TheVal)
) + 1 AS Rank
FROM
q2b_Uni AS a
ORDER BY
a.ID, a.TheVal;
Is there a more efficient way to go directly from tbl1 to tbl3?
Thanks in advance!