访问/喷射相当于 Oracle 的解码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1763228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 19:28:05  来源:igfitidea点击:

Access/jet equivalent of Oracle's decode

sqloraclems-accessjet

提问by René Nyffenegger

Is there an equivalent for Oracle's decode() in Access (or Jet, for that matter).

在 Access(或 Jet,就此而言)中是否有 Oracle 的 decode() 等价物。

The problem I am facing is: I should sort (order) a resultset based basically upon a status and a date (with all records having status = 2) at the end.

我面临的问题是:我应该在最后根据状态和日期(所有记录的状态 = 2)对结果集进行排序(排序)。

In Oracle I'd go something like

在甲骨文我会去类似

select
  ...
from
  ...
where
  ..
order by
  decode(status, 2, 0, 1),
  date_column

回答by onedaywhen

The closest analogy is the SWITCH()function e.g.

最接近的类比是SWITCH()函数,例如

Oracle:

甲骨文:

SELECT supplier_name,
       decode(supplier_id,  10000, 'IBM',
                            10001, 'Microsoft',
                            10002, 'Hewlett Packard',
                                   'Gateway') result
  FROM suppliers;

Access Database Engine

访问数据库引擎

SELECT supplier_name,
       SWITCH(supplier_id = 10000, 'IBM',
              supplier_id = 10001, 'Microsoft',
              supplier_id = 10002, 'Hewlett Packard',
              TRUE, 'Gateway') AS result
  FROM suppliers; 

Note that with the SWITCH()function you have to supply the full predicate each time, so you are not restricted to using just supplier_id. For the default value, use a predicate that is obvious to the human reader that it is TRUE e.g. 1 = 1or indeed simply TRUE:)

请注意,对于该SWITCH()函数,您每次都必须提供完整的谓词,因此您不仅限于使用供应商 ID。对于默认值,使用一个对人类读者来说显而易见的谓词,例如它是 TRUE1 = 1或者实际上只是TRUE:)

Something that may not be obvious is that the logic in the SWITCH()function doesn't short circuit, meaning that every expression in the function must be able to be evaluated without error. If you require logic to short circuit then you will need to use nested IIF()functions.

可能不明显的是,SWITCH()函数中的逻辑不会短路,这意味着函数中的每个表达式都必须能够正确计算。如果您需要逻辑短路,那么您将需要使用嵌套IIF()函数。

回答by Pablo Santa Cruz

You can try with IIF. See thisstackoverflow question.

你可以试试 IIF。请参阅stackoverflow 问题。

回答by Fionnuala

I think it might compare to switch or choose.

我认为它可以比较切换或选择。

Switch(expr-1, value-1[, expr-2, value-2 … [, expr-n,value-n]])

-- http://office.microsoft.com/en-us/access/HA012289181033.aspx

-- http://office.microsoft.com/en-us/access/HA012289181033.aspx

Choose(index, choice-1[, choice-2, ... [, choice-n]])

-- http://msdn.microsoft.com/en-us/library/aa262690%28VS.60%29.aspx

-- http://msdn.microsoft.com/en-us/library/aa262690%28VS.60%29.aspx

回答by elrado

You can use the SWITCHfunction:

您可以使用该SWITCH功能:

LABEL: Switch(
   [TABLE_NAME]![COL_NAME]='VAL1';'NEW_VAL1';
   [TABLE_NAME]![COL_NAME]='VAL2';'NEW_VAL2';
)

Note semicolons and not commas.

注意分号而不是逗号。

The example above works in queries in MS Access 2010.

上面的示例适用于 MS Access 2010 中的查询。