hello to all
I am asking for help with a problem I'm having with using SELECT FIRST in a TOTALs query.
Here's the situation:
a) In VBA, I generate code to INSERT INTO the table shown below in b). So far, so good.
TABLE1
======
etc...
Note that for each (ID_X,ID_Y) combination Val2 will always the same 7 pairs of numbers
differing only in their order.
c) Now I want to GROUP BY ID_X, ID_Y, Val2 and determine the value of a new field, Z as shown
in the SQL below.
d) The desired output is...
TABLE2
======
etc...
e) FINALLY - here is the problem!!!
*** I use this code on very large tables, and it works 99.999% of the time. But on rare occasion I get an error in the Z column (1 instead of 0, and v.v.)
Also, the error seems sporadic - I can rerun the same code only to see an error in a record that was previously correct!
I'm sure that the problem is that FIRST really only has meaning in an ORDERed list. But, this is a TOTALs query, so I don't really have any columns I can use to 'nail down' the order.
Any help will be greatly appreciated. It's been hard to even narrow down the error source this far, because the errors occur so infrequently. Vicky
I am asking for help with a problem I'm having with using SELECT FIRST in a TOTALs query.
Here's the situation:
a) In VBA, I generate code to INSERT INTO the table shown below in b). So far, so good.
TABLE1
======
Code:
b) AutoID ID_X ID_Y Val1 Val2 (nb: Val2 = 1 + ABS(Val1))
=======================================
1001 56 288 2 3
1002 56 288 8 9
1003 56 288 32 33
1004 56 288 -4 5
1005 56 288 -8 9
1006 56 288 -2 3
1007 56 288 4 5
1008 56 288 16 17
1009 56 288 -32 33
1010 56 288 -16 17
1011 56 289 2 3
1012 56 289 8 9
1013 56 289 -32 33
1014 56 289 -4 5
1015 56 289 -8 9
1016 56 289 -2 3
1017 56 289 4 5
1018 56 289 32 33
1019 56 289 -16 17
1020 56 289 16 17
etc...
Note that for each (ID_X,ID_Y) combination Val2 will always the same 7 pairs of numbers
differing only in their order.
c) Now I want to GROUP BY ID_X, ID_Y, Val2 and determine the value of a new field, Z as shown
in the SQL below.
Code:
SELECT T.ID_X, T.ID_Y, T.BVal2, IIf(FIRST(T.Val1) > 0, 1, 0) AS Z
FROM TABLE1 T
GROUP BY T.ID_X, T.ID_Y, T.BVal2
ORDER BY T.ID_X, T.ID_Y, T.BVal2;
d) The desired output is...
TABLE2
======
Code:
ID_X ID_Y Val2 Z
===================================================
56 288 3 1 because the 2 comes before the -2 in TABLE1
56 288 5 0 because the -4 comes before the 4 in TABLE1
56 288 9 1 etc...
56 288 17 1
56 288 33 1
56 289 3 1
56 289 5 0
56 289 9 1
56 289 17 0
56 289 33 0
etc...
e) FINALLY - here is the problem!!!
*** I use this code on very large tables, and it works 99.999% of the time. But on rare occasion I get an error in the Z column (1 instead of 0, and v.v.)
Also, the error seems sporadic - I can rerun the same code only to see an error in a record that was previously correct!
I'm sure that the problem is that FIRST really only has meaning in an ORDERed list. But, this is a TOTALs query, so I don't really have any columns I can use to 'nail down' the order.
Any help will be greatly appreciated. It's been hard to even narrow down the error source this far, because the errors occur so infrequently. Vicky