1

我需要一个参数化的交叉表查询。除了参数部分,我大部分时间都解决了。我有四个要应用的参数:ChangeID 和variant1、variant2 和variant3。ChangeID 为必填项,需要用户填写。Variant1..3 是可选的:用户可以填写一到三个变体,或者(如果 variant1 为空)应使用默认值。

默认是预定义变体的替代,实际填写的变体也应作为替代应用。

我想到了这样的事情:

PARAMETERS ChangeID Text (7), variant1 Text (10), variant2 Text (10), variant3 Text (10);
TRANSFORM Sum([Qty]*[inout]) AS QtyInOut
SELECT MatByOrder.CHpoint, MatByOrder.PN, MatByOrder.Description, MatByOrder.Price, MatByOrder.UoM
FROM Variants INNER JOIN MatByOrder ON Variants.BO = MatByOrder.BO

WHERE 
    (MatByOrder.CHpoint=ChangeID) AND 
    (IIF(isNull([variant1]); 
        (Variants.Variant="B100") OR (Variants.Variant="B200") OR (Variants.Variant="B300") OR (Variants.Variant="B400") OR (Variants.Variant="B500");
        (Variants.Variant=[variant1]) OR (Variants.Variant=[variant2]) OR (Variants.Variant=[variant3])
       )
    )

GROUP BY MatByOrder.CHpoint, MatByOrder.PN, MatByOrder.Description, MatByOrder.Price, MatByOrder.UoM

但它不起作用。问题当然出在 WHERE 子句中的 IIF 中。

实现此目的的正确方法是什么?

4

2 回答 2

1

这应该有效:

WHERE 
    (MatByOrder.CHpoint=ChangeID) AND 
    (
       ( ([variant1] IS NULL) AND (Variants.Variant IN ('B200', 'B300', 'B400', 'B500')) )
       OR    
       ( ([variant1] IS NOT NULL) AND (Variants.Variant IN ([variant1], [variant2], [variant3])) )
    )
于 2012-05-31T10:39:55.870 回答
0

我已经在我的语言环境中测试了 Instr 的想法,其中逗号是参数分隔符,它对我有用。我认为你应该用简单的查询来尝试这个想法,看看它是否适合。

这个想法是:

Instr(Nz(Var,String) & ",",Field & ",")>0

在字段中添加逗号或其他分隔符对于避免错误匹配很重要。

PARAMETERS ChangeID Text (7), variant1 Text (10), variant2 Text (10), variant3 Text (10);
TRANSFORM Sum([Qty]*[inout]) AS QtyInOut
SELECT MatByOrder.CHpoint, MatByOrder.PN, MatByOrder.Description, MatByOrder.Price, MatByOrder.UoM
FROM Variants INNER JOIN MatByOrder ON Variants.BO = MatByOrder.BO

WHERE 
    (MatByOrder.CHpoint=ChangeID) AND 
    (Instr(Nz([variant1],"B100,B200,B300,B400,B500" 
               & [variant1] & "," & [variant2] & "," 
               & [variant3]) & ",", MatByOrder.CHpoint) > 0
    )

GROUP BY MatByOrder.CHpoint, MatByOrder.PN, MatByOrder.Description, MatByOrder.Price, MatByOrder.UoM
于 2012-05-31T09:34:09.423 回答