简体   繁体   English

相当于Oracle的Access / jet解码

[英]Access/jet equivalent of Oracle's decode

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 在Oracle中,我会像

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

The closest analogy is the SWITCH() function eg 最接近的类比是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. 请注意,使用SWITCH()函数时,您每次都必须提供完整的谓词,因此,您不仅限于使用vendor_id。 For the default value, use a predicate that is obvious to the human reader that it is TRUE eg 1 = 1 or indeed simply TRUE :) 对于默认值,请使用对人类读者显而易见的谓词为TRUE的谓词,例如1 = 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. 可能不明显的是SWITCH()函数中的逻辑不会短路,这意味着该函数中的每个表达式都必须能够正确地求值。 If you require logic to short circuit then you will need to use nested IIF() functions. 如果需要逻辑短路,则需要使用嵌套的IIF()函数。

You can try with IIF. 您可以尝试使用IIF。 See this stackoverflow question. 请参阅 stackoverflow问题。

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

You can use the SWITCH function: 您可以使用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中的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM