所以我的表格格式如下:
Project | type1 | type2 | type3 | type4
a | 0 | 1 | 1 | 2
b | 1 | 0 | 1 | 1
c | 0 | 0 | 0 | 0
我需要每行的最大值,如下所示:
Project | max
a | 2
b | 1
c | 0
我无法在访问中使用switch()函数,因为我有太多的变量而且它给出了“太复杂”的错误。有任何想法吗?
SELECT Switch(
[MaxOfBudget Trigger] = 2, "Critical",
[MaxOfSchedule Trigger] = 2, "Critical",
[MaxOfSubmittals Trigger] = 2, "Critical",
[MaxOfSafety Trigger] = 2, "Critical",
[MaxOfChange Orders Trigger] = 2, "Critical",
[MaxOfContingency Trigger] = 2, "Critical",
[MaxOfRFIs Trigger] = 2, "Critical",
[MaxOfBudget Trigger] = 1, "At Risk",
[MaxOfSchedule Trigger] = 1, "At Risk",
[MaxOfSubmittals Trigger] = 1, "At Risk",
[MaxOfSafety Trigger] = 1, "At Risk",
[MaxOfChange Orders Trigger] = 1, "At Risk",
[MaxOfContingency Trigger] = 1, "At Risk",
[MaxOfRFIs Trigger] = 1, "At Risk",
[MaxOfBudget Trigger] = 0, "Okay",
[MaxOfSchedule Trigger] = 0, "Okay",
[MaxOfSubmittals Trigger] = 0, "Okay",
[MaxOfSafety Trigger] = 0, "Okay",
[MaxOfChange Orders Trigger] = 0, "Okay",
[MaxOfContingency Trigger] = 0, "Okay",
[MaxOfRFIs Trigger] = 0, "Okay", )
AS test, [Project Triggers].[Project Number]
FROM [Project Triggers];
答案 0 :(得分:2)
您的数据库没有按照应有的方式进行标准化。
您应该有一个type
表,其中包含值1,2,3,4,然后是project
表的连接表。
|project | type_id | value |
+--------+---------+-------+
|a | 1 | 0 |
|a | 2 | 1 |
|a | 3 | 1 |
|a | 4 | 2 |
|b | 1 | 1 |
|b | 2 | 0 |
|b | 3 | 1 |
|b | 4 | 1 |
|c | 1 | 0 |
|c | 2 | 0 |
|c | 3 | 0 |
|c | 4 | 0 |
然后,您可以执行SELECT project, max(value) FROM project_type_values GROUP BY project;
之类的操作来获得结果。
答案 1 :(得分:2)
我完全同意Daniël的回答,我希望您能够修改数据模型。但是如果你不能,你可以声明一个可以返回最大数字列表的函数:
Function MaxOfList(ParamArray varValues()) As Variant
Dim i As Integer 'Loop controller.
Dim varMax As Variant 'Largest value found so far.
varMax = Null 'Initialize to null
For i = LBound(varValues) To UBound(varValues)
If varMax >= varValues(i) Then
'do nothing
Else
varMax = varValues(i)
End If
Next
MaxOfList = varMax
End Function
稍高级版本以及Min
版本可以在Allen Browne's Access Tips: MinOfList() and MaxOfList() functions