Am trying to categorise values using Ms Access

人走茶凉 提交于 2019-12-12 03:27:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!