简体   繁体   English

选择字段总和最大的记录 (Access 2010)

[英]Select a record with the largest sum of fields (Access 2010)

I would like to select a record from a table based on the field “labcode” specified by the user on a form.我想根据用户在表单上指定的字段“labcode”从表中选择一条记录。 There could be multiple records associated with each “labcode” and I would like to select a record that has the highest sum of 10 corresponding fields in the “tblDSA". Fields are named as follows: “A1_MFI”, “A2_MFI”, “C1_MFI”, "C2_MFI", "DR1_MFI", "DR2_MFI"…)每个“labcode”可能有多个记录,我想选择“tblDSA”中10个对应字段总和最高的记录。字段命名如下:“A1_MFI”、“A2_MFI”、“C1_MFI” ”、“C2_MFI”、“DR1_MFI”、“DR2_MFI”……)

All 10 fields are in 'text' format and sometimes contains a number, text or are left blank.所有 10 个字段都是“文本”格式,有时包含数字、文本或留空。 I would only like to sum up records that contain a number in that field.我只想总结在该字段中包含数字的记录。 Do I need to create a new field in “tblDSA” that holds the total score or should I avoid storing calculating values in the table?我是否需要在“tblDSA”中创建一个新字段来保存总分,还是应该避免在表中存储计算值?

Dim SQL As String
Dim db As DAO.Database
Dim tblDSA As DAO.Recordset

Set db = CurrentDb

Set tblDSA = db.OpenRecordset("tblDSA")

   SQL = "SELECT * Nz((Val[A1_MFI])) + Nz((Val[A2_MFI])) + Nz((Val[B1_MFI])) + Nz((Val[B2_MFI])) +  Nz((Val[C1_MFI])) + Nz((Val[C2_MFI])) + Nz((Val[DR1_MFI]))+ Nz((Val[DR2_MFI])) + Nz((Val[DQB1_MFI] + Nz((Val[DQB2_MFI]))as TotalScore FROM tblDSA WHERE [LABCODE] = " & Me.tbLabcode.Value & " ORDER BY TotalScore DESC "


Debug.Print SQL

Set rs = db.OpenRecordset(SQL)

The SQL above contains a syntax error (missing operator), therefore, I can't test it.上面的 SQL 包含语法错误(缺少运算符),因此,我无法对其进行测试。 I'm not sure what is missing?我不确定缺少什么? Nz() is for skipping blank records and Val() is to convert each text field into value. Nz() 用于跳过空白记录,Val() 用于将每个文本字段转换为值。 Please let me know if this is a correct approach or I need to do something else?请让我知道这是正确的方法还是我需要做其他事情? Thanks谢谢

It looks like you two things看起来你有两件事

  • you didn't have a comman after "SELECT *"你在“SELECT *”之后没有命令
  • missing two brackets in one of your NZ statements在您的新西兰声明之一中缺少两个括号

@PhillipXT pointed out the first - and by using his second suggestion, I think the SQL compiler would have pinpointed the missing brackets for you. @PhillipXT 指出了第一个 - 通过使用他的第二个建议,我认为 SQL 编译器会为您指出缺少的括号。

Try this with a copy / paste用复制/粘贴试试这个

SQL = "SELECT *, Nz((Val[A1_MFI])) + Nz((Val[A2_MFI])) + Nz((Val[B1_MFI])) + _
Nz((Val[B2_MFI])) +  Nz((Val[C1_MFI])) + Nz((Val[C2_MFI])) + Nz((Val[DR1_MFI])) + _   
Nz((Val[DR2_MFI])) + Nz((Val[DQB1_MFI])) + Nz((Val[DQB2_MFI])) AS TotalScore _   
FROM tblDSA _      
WHERE [LABCODE] = " & Me.tbLabcode.Value & _    
" ORDER BY TotalScore DESC "   

Okay, after much back and forth, here is the final result that works for this particular problem:好的,经过多次反复之后,这是适用于这个特定问题的最终结果:

SELECT TOP 1 *, (Nz(Val(IIf([A1_MFI] Is Null, 0, [A1_MFI]))) + Nz(Val(IIf([A2_MFI] Is Null, 0, [A2_MFI]))) + ...) AS TotalScore
FROM tblDSA
WHERE [LABCODE] = 57
ORDER BY (Nz(Val(IIf([A1_MFI] Is Null, 0, [A1_MFI]))) + Nz(Val(IIf([A2_MFI] Is Null, 0, [A2_MFI]))) + ...) DESC

I thought Access allowed field aliases in the ORDER BY, but it doesn't seem to do that any more, if it did at all.我认为 Access 允许 ORDER BY 中的字段别名,但它似乎不再这样做了,如果它确实这样做的话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM