SQL / MS Access - 为什么这个CASE不工作?

时间:2013-07-02 17:33:24

标签: sql ms-access case

我正在编写一个查询,根据名为“USERDATE”的用户定义日期确定要使用的价格。当我运行它时,我在表达式中得到“语法错误(缺少运算符)”。我希望选择的价格是“UNIT_AMT”,稍后将在查询中使用。

SELECT MDDB.NDC, CARRIER.GROUP_ID, [USERDATE] AS USERDATE, MDDB.CODE, CARRIER.DUR_MU_R, CARRIER.FEE_BR, CARRIER.FEE_GR, CARRIER.AWP_BR, CARRIER.AWP_GR, CARRIER.DUR_MU_M, CARRIER.FEE_BM, CARRIER.FEE_GM, CARRIER.AWP_BM, CARRIER.AWP_GM, MDDB.AWP_UNIT_AMT, 

CASE
WHEN USERDATE >= AWP_EFF_DATE THEN AWP_UNIT_AMT
WHEN USERDATE >= AWP_1ST_OLDEST_EFF_DATE AND USERDATE < AWP_EFF_DATE THEN AWP_1ST_OLDEST_UNIT_AMT
WHEN USERDATE >= AWP_2ND_OLDEST_UNIT_AMT AND USERDATE < AWP_1ST_OLDEST_UNIT_AMT THEN AWP_2ND_OLDEST_UNIT_AMT
WHEN USERDATE >= AWP_3RD_OLDEST_UNIT_AMT AND USERDATE < AWP_2ND_OLDEST_UNIT_AMT THEN AWP_3RD_OLDEST_UNIT_AMT
WHEN USERDATE >= AWP_4TH_OLDEST_UNIT_AMT AND USERDATE < AWP_3RD_OLDEST_UNIT_AMT THEN AWP_4TH_OLDEST_UNIT_AMT
WHEN USERDATE >= AWP_5TH_OLDEST_UNIT_AMT AND USERDATE < AWP_4ST_OLDEST_UNIT_AMT THEN AWP_5TH_OLDEST_UNIT_AMT
ELSE AWP_UNIT_AMT
END AS UNIT_AMT,

((([UNIT_AMT]*[QTY])*((100+[AWP_GR])/100))+[DUR_MU_R]+[FEE_GR]) AS [GR PRICE], 
((([UNIT_AMT]*[QTY])*((100+[AWP_BR])/100))+[DUR_MU_R]+[FEE_BR]) AS [BR PRICE], 
((([UNIT_AMT]*[QTY])*((100+[AWP_GM])/100))+[DUR_MU_M]+[FEE_GM]) AS [GM PRICE], 
((([UNIT_AMT]*[QTY])*((100+[AWP_BM])/100))+[DUR_MU_M]+[FEE_BM]) AS [BM PRICE]
FROM MDDB_MASTER AS MDDB, CARRIER_GROUP AS CARRIER
WHERE (((MDDB.NDC)="41520091574") AND ((CARRIER.GROUP_ID)="709608"));

感谢您的时间和帮助!

1 个答案:

答案 0 :(得分:2)

由于Access SQL不支持CASE ... WHEN,因此您可以使用嵌套的IIf()表达式。但是,有这么多条件,嵌套IIf()将具有挑战性。在这种情况下,您可能会发现Switch()更容易使用。

Switch(
    USERDATE >= AWP_EFF_DATE, AWP_UNIT_AMT,
    USERDATE >= AWP_1ST_OLDEST_EFF_DATE AND USERDATE < AWP_EFF_DATE, AWP_1ST_OLDEST_UNIT_AMT,
    USERDATE >= AWP_2ND_OLDEST_UNIT_AMT AND USERDATE < AWP_1ST_OLDEST_UNIT_AMT, AWP_2ND_OLDEST_UNIT_AMT,
    USERDATE >= AWP_3RD_OLDEST_UNIT_AMT AND USERDATE < AWP_2ND_OLDEST_UNIT_AMT, AWP_3RD_OLDEST_UNIT_AMT,
    USERDATE >= AWP_4TH_OLDEST_UNIT_AMT AND USERDATE < AWP_3RD_OLDEST_UNIT_AMT, AWP_4TH_OLDEST_UNIT_AMT,
    USERDATE >= AWP_5TH_OLDEST_UNIT_AMT AND USERDATE < AWP_4ST_OLDEST_UNIT_AMT, AWP_5TH_OLDEST_UNIT_AMT,
    True, AWP_UNIT_AMT
) AS UNIT_AMT