简体   繁体   English

MS Access SQL IIF优化

[英]MS Access SQL IIF optimization

Given this table, I have a query that uses IIF to pull out the value from one column when the other one is NULL. 给定此表,我有一个查询,当另一列为NULL时,该查询使用IIF从一列中提取值。

COL_A,COL_B
NULL,10
12,NULL
14,NULL
NULL,16

SELECT iif(COL_A IS NULL, COL_B, COL_A);

Results:
10
12
14
16

What I'm trying to work out is how to do this in straight SQL. 我要解决的问题是如何在直接SQL中执行此操作。 Is there a way this can be done? 有办法吗? Normally I wouldn't mind using this a few times, but we have a massive query with cough 86 IIF statements and it's getting prohibitive to run. 通常我不会介意使用了几次,但我们有咳嗽 86 IIF语句大规模查询,它变得高昂的运行。 Any ideas? 有任何想法吗?

您可以使用NZ()

 select nz(COL_A, COL_B)

If your query needs to carry out 86 different test's in one query then I would suggest the data architecture is flawed, you could shorten down your SQL by writing a custom function using the "Case" method which could step through all the validation and return a result. 如果您的查询需要在一个查询中执行86个不同的测试,那么我认为数据体系结构存在缺陷,您可以通过使用“ Case”方法编写自定义函数来缩短SQL的过程,该方法可以逐步完成所有验证并返回结果。 However this will only improve the look of your SQL and won't really improve performance. 但是,这只会改善SQL的外观,并不会真正改善性能。 Difficult to say without a list of the 86 IIF's but I think you need to look at your data structure to see if you can improve things from table level. 没有列出86个IIF的列表很难说,但我认为您需要查看数据结构以查看是否可以从表级别进行改进。

Assuming only one column will have an actual value, You could just Concatenate them, since the nulls would compress. 假设只有一列具有实际值,则可以将它们串联起来,因为空值会压缩。 Select COL_A||COL_B||COL_C||COL_D||.... 选择COL_A||COL_B||COL_C||COL_D||....

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

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