问题
I am trying to categorise values using Ms Access into three categories; lets say (less than 3500 to be low, from 35000 to 1,500,000 to be middle and greater than 1,500,000 to be Upper). Please help I am still a learner
回答1:
Create a select query with all the fields you need using the simple query wizard, then open it in design view and add a new column and type in this text (use your numeric fields name between the square brackets, I called mine "number")
category: IIf([number]<3500,1,IIf([number]<1500000,2,3))
This will return the value 1 (the first condition is true),2 (the second condition is true) or 3 (first and second conditions were false) depending on the value of the [number] field. This could return a text value by replacing the numbers with "text" values.
category: IIf([number]<3500,"Low",IIf([number]<1500000,"Middle","Upper"))
This query can then be used wherever you need this [category] field. Adding more categories just makes the nested Iif longer. I used < (less than) rather than <= less than and equal, adjust as necessary. I also used 3500 rather than 35000 as the number for the difference between low and middle.
If you only need it once you could add this same information as a calculated field on a form or table but a query means you can easily change it and have the change reflected everywhere you use it.
Using the switch statement instead of nested iif (as suggested by Don George) the field definition would be
category2: Switch([number]<3500,"Low",[number]<1500000,"Middle",[number]>=1500000,"Upper")
来源:https://stackoverflow.com/questions/36952421/am-trying-to-categorise-values-using-ms-access