N
NY
Dear all,
I am writing code about custom sorting tecniques. This code will appear
in my upcoming book. I explain two ways: one by using the switch function
and the other by using a lookup table. Although both solutions work as they
are supposed to, with respect to the sorting criterion (state), the records
WITHIN each state do not appear in the same exact order. I am fried to think
anymore at this point. I wrote too much code.
This is the custom sorting order using switch
SELECT *
FROM Qry_Conditions
WHERE STATE in ("NY","CA","TX")
ORDER BY
SWITCH(
[state]= 'NY', 1,
[state]= 'CA', 2,
[state] = 'TX', 3
)
This is the custom sorting order using a lookup table
SELECT * FROM Qry_Conditions
INNER JOIN tblS_State ON Qry_Conditions.state = tblS_State.State;
The tbls_State contains:
1 NY
2 CA
3 TX
If you want to download the database to play with the code, you can do that
here:
http://www.databasechannel.com/sampledata/Access2007/data.html
Any suggestions will be highly appreciated.
My best
Pinda
I am writing code about custom sorting tecniques. This code will appear
in my upcoming book. I explain two ways: one by using the switch function
and the other by using a lookup table. Although both solutions work as they
are supposed to, with respect to the sorting criterion (state), the records
WITHIN each state do not appear in the same exact order. I am fried to think
anymore at this point. I wrote too much code.
This is the custom sorting order using switch
SELECT *
FROM Qry_Conditions
WHERE STATE in ("NY","CA","TX")
ORDER BY
SWITCH(
[state]= 'NY', 1,
[state]= 'CA', 2,
[state] = 'TX', 3
)
This is the custom sorting order using a lookup table
SELECT * FROM Qry_Conditions
INNER JOIN tblS_State ON Qry_Conditions.state = tblS_State.State;
The tbls_State contains:
1 NY
2 CA
3 TX
If you want to download the database to play with the code, you can do that
here:
http://www.databasechannel.com/sampledata/Access2007/data.html
Any suggestions will be highly appreciated.
My best
Pinda