0

我有两列名为 pno 和 rno 都是 varchar 有一个订单号,现在我的要求是

如果 rno 不为 null,则应打印Actual Generated,如果不是,则为Forecast Generated

如果 pno 不为空,那么它应该打印 Reconciled else Forecast Generated

我的查询是:

 select  
       case
        when (pno!=null) then 'Reconciled'
       when (pno=null) then 'Forecast1 Generated'
       when (rno=null) then 'Forecast Generated'
       when (rno!=null) then 'Actual Generated'
       end as Status
 from tablexyz

当我执行它时,我只得到 pno 或 rno case 语句结果集。我的预期输出应该返回所有 case 语句。我无法使用 and in case 语句条件,因为我需要所有记录。我在 Dbvisualizer 上运行它。

提前致谢。

4

1 回答 1

0

在SQL中应该使用pno is not nullinstead。SQLpno!=null使用三值逻辑,并且pno!=null是UNKNOWN,既不真也不假,但由于不真,所以不采取这种情况。

所以你的陈述应该看起来像

select  
       case
       when (pno is not null) then 'Reconciled'
       when (pno is null) then 'Forecast1 Generated'
       when (rno is null) then 'Forecast Generated'
       when (rno is not null) then 'Actual Generated'
       end as Status
 from tablexyz
于 2013-08-16T20:36:54.147 回答