我正在编写查询以获取金额为>的所有发票100为美国> 200为CAN和> 1200为mxn(墨西哥)
Invoice_Header表(总金额和公司ID) 公司表(公司ID和货币ID) 货币表(货币ID和货币)
SELECT IH.Invoice_Num , IH.invoice_amount , IH.invoice_date
FROM Invoice_Header IH ,
company c,
Currency cu
WHERE IH.Company_oid = c.Company_oid
AND c.currency_oid = cu.currency_oid
AND
CASE IH.total =
WHEN cu.code = 'USA' then IH.total > 100
WHEN cu.code = 'CAN' then IH.total > 200
WHEN cu.code = 'MXN' then IH.total > 1200
END
尝试在oracle数据库上运行时遇到语法错误。
答案 0 :(得分:6)
我不会在这里使用CASE
声明。听起来你想要一些基本的布尔逻辑
AND( (cu.code = 'USA' and ih.total > 100)
OR (cu.code = 'CAN' and ih.total > 200)
OR (cu.code = 'MXN' and ih.total > 1200))
如果您出于某种原因真的想要使用CASE
语句,可以执行类似
AND 1 = (CASE WHEN cu.code = 'USA' and ih.total > 100
THEN 1
WHEN cu.code = 'CAN' and ih.total > 200
THEN 1
WHEN cu.code = 'MXN' and ih.total > 1200
THEN 1
ELSE 0
END)
答案 1 :(得分:1)
我会尝试像
这样的东西 SELECT IH.Invoice_Num , IH.invoice_amount , IH.invoice_date
FROM Invoice_Header IH ,
company c,
Currency cu
WHERE IH.Company_oid = c.Company_oid
AND c.currency_oid = cu.currency_oid
AND ( (cu.code = 'USA' and IH.total > 100)
OR (cu.code = 'CAN') and IH.total > 100)
OR (cu.code = 'MXN') and IH.total > 1200))
如果你真的需要在where子句中使用CASE,你可以做类似的事情
SELECT 'WEEK-END' AS col_a
FROM DUAL
WHERE 1 = CASE
WHEN to_char(sysdate, 'Day') = 'Saturday' then 1
WHEN to_char(sysdate, 'Day') = 'Sunday' then 1
else 0
END