【问题标题】:Matching similar/alike column entrys in MS Access 2003在 MS Access 2003 中匹配相似/相似的列条目
【发布时间】:2012-08-09 12:47:36
【问题描述】:

我所追求的是在 MS Access 中使用 sql 的基础工作量摘要,这就是我想要做的:

Sum(IIf(([Time Report].Destination) Like 'D-[Time Report].[Baselocation]',0)) AS [Total WorkFromBase in P2]

这不起作用,因为 MS Access 不理解正则表达式,我需要附加“D-”才能匹配。

据我所知,我的选择是:

  1. 学习和使用 VB 宏 (?) 来正确匹配模式
  2. 使用一组复杂的 IIf 语句,因为 sql 没有原生 else if 条件

我不懂VB,只见过example,看不懂。

如果我选择大量 IIf,那么我就有类似的东西

Sum(
    IIf(D-[Time Report].[Baselocation] = 'Base1', IIf(
        ([Time Report].Destination) = 'D-Base1',
    IIf(D-[Time Report].[Baselocation] = 'Base2', IIf(
        ([Time Report].Destination) = 'D-Base2',
    IIf(D-[Time Report].[Baselocation] = 'Base3a', IIf(
        ([Time Report].Destination) = 'D-Base3a' OR ([Time Report].Destination) = 'D-Base3b',
    IIf(D-[Time Report].[Baselocation] = 'Base3b', IIf(
        ([Time Report].Destination) = 'D-Base3a' OR ([Time Report].Destination) = 'D-Base3b',    ))
)) AS [Total Work From Base in P1],

然后我得到一个Syntax Error (Missing operator) 类型错误,

那么我怎样才能匹配两个列,并在它们相似时求和?

【问题讨论】:

    标签: sql regex ms-access


    【解决方案1】:

    ...我需要附加“D-”才能匹配。

    您可以将“D-”连接到[Baselocation],然后使用等号而不是Like 比较。

    [Time Report].Destination) = "D-" & [Time Report].[Baselocation]
    

    IIf() 表达式包含 3 个参数。

    IIf(expr, truepart, falsepart)
    

    在您的示例中,您仅提供了 2 个参数 AFAICT。我认为您希望Sum()exprTrue 时包含一些值([amount worked]?),但在exprFalse 时为零。可能是这样的……

    Sum(IIf(([Time Report].Destination) = "D-" & [Time Report].[Baselocation], [amount worked, 0)) AS [Total WorkFromBase in P2]
    

    【讨论】:

    • 这就是我现在拥有的 Sum(IIf(([Time Report].Destination) = "D-" & [QRY7 Ready for Time Report].[Baselocation],[Time Report].[Hrs P1], 0)) AS [Total WorkFromBase in P1]WHERE (([Time Report].Destination) = "D-" & [Time Report].[Baselocation]) 选择后,所以我没有得到所有带有“D-”的东西。否则。完美!
    【解决方案2】:

    为什么没有一个可能性表并在您的查询中引用它?

     If D-[Time Report].[Baselocation] = 'Base1' IN (SELECT Bases FROM NewTable)
    

    或者

     SELECT D-[Time Report].[Baselocation], NewTable.Destination 
     FROM D-[Time Report] 
     INNER JOIN Newtable
     ON D-[Time Report].[Baselocation] = NewTable.Bases 
    

    其中 NewTable 包含位置列表以及该基本映射到的位置。

    您也可以使用 LIKE 或 ALIKE 运算符:

    IIf(D-[Time Report].[Baselocation] LIKE '*Base1*'
    

    一些参考资料:

    Fundamental Microsoft Jet SQL for Access 2000
    Intermediate Microsoft Jet SQL for Access 2000
    Advanced Microsoft Jet SQL for Access 2000

    【讨论】:

    • 这似乎有点矫枉过正,新表只是一个基地到位置的地图,除了前面加了“D-”之外,几乎相同。另外我不遵循您的变量名...table.Stuff?我之前也从未使用过内部连接,您能解释一下您是如何使用它的吗?
    • 我添加了另一种可能更适合您的可能性。如果你打算继续使用 SQL,你需要做一些阅读。您可以使该查询更简单。 BTW table.Stuff 只是表示从表中选择的任何字段的注释。
    猜你喜欢
    • 2022-08-18
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 2012-12-02
    • 1970-01-01
    相关资源
    最近更新 更多