繁体   English   中英

在 where 子句中输入过滤器后,当我的查询没有返回任何内容时,如何 output 默认结果?

[英]How to output a default result when my query returns nothing after entering filters in the where clause?

我有几个复杂的查询,并且必须对内容通用,但要点是我们有一个使用查询生成表格仪表板的系统。 该系统的一部分允许我们在 Where 子句中插入日期或值等过滤器。 过滤时,查询可能包含也可能不包含基于这些过滤器的结果。

这是一个非常普遍的例子。 过滤器由 [[]] 表示。

表:

记录ID column_a column_b 列_c
1001
1002
1003
1004

我的查询是:

select recordid is ID, column_a as A, column_b as B, column_c as C

from mytable

where [[column_a = (value picked from list)]] and [[column_b = (value picked from list)]]

我想知道是否有一种方法可以解决Z78E6221F6393D135681DB398F14CE6DZ当所选过滤器导致空集时某种东西? 我知道如何使用不同的函数来检查列值是否为空/空,但这并没有得到我需要的结果。 我不知道是否有办法检查查询的 output,看不到任何结果,然后 output 一行值,如“null”或“n/a”或我的客户在他们的 UI 上看到的东西。

例如,如果有人做了以下事情:

select recordid as ID, column_a as A, column_b as B, column_c as C

from mytable

where [[column_a = one]] and [[column_b = one]]

这将导致空结果。 我想它是否可以 output 类似:

记录ID column_a column_b 列_c
不适用 不适用 不适用 不适用

我什至不确定这是否可能。 欢迎任何想法/建议。

这更像是一个合乎逻辑的场景。 他们有多种方法可以做到这一点。 以下是可以使用的概念。

---Get count of query and if it is 0 then display 'na'
DECLARE @count as int=(
select count(1) from (
select * from table3
where column_a = 'one' and column_b = 'two' ) temp);

if @count=0
select top 1 'na' as recordid , 'na' as column_a, 'na' as column_b, 'na' as column_c  from table3

if @count<>0
select * from table3
where column_a = 'one' and column_b = 'two'

应该有其他更有效的方法,但你可能会明白这个概念。

暂无
暂无

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

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