0

我有一个 MS 查询,它选择两个不同记录的计数,然后执行计算以从另一个结果中减去一个结果并将结果显示为difference.

见下文:

Ticketnumber   |   Createdon    |    Modifiedon  |
00012121       |    16/03/2015  |    15/03/2015  |
00211335       |    10/03/2015  |    17/03/2015  |

SELECT  ( SELECT    COUNT(incidentbase.ticketnumber) AS val2
          FROM      IncidentBase
          WHERE     createdon >= GETDATE() - 7
        ) - ( SELECT    COUNT(incidentbase.ticketnumber) AS val1
              FROM      incidentbase
              WHERE     modifiedon >= GETDATE() - 7
            ) AS difference

我想要的是将结果显示为单个值,具体取决于结果是正数还是负数,类似于:

WHEN difference => 0 then 1
WHEN difference =< 0 then 0 

有人可以帮忙吗?

4

2 回答 2

2

只需使用CASE

SELECT CASE 
          WHEN (SELECT COUNT (incidentbase.ticketnumber) as val2 
                FROM IncidentBase 
                WHERE createdon >= GETDATE()-7) - 
               (SELECT count (incidentbase.ticketnumber) as val1 
                FROM incidentbase 
                WHERE modifiedon >= GETDATE()-7) <= 0 THEN 1
          ELSE 0 
       END AS flag 
于 2015-03-18T13:41:59.830 回答
0

您可以像这样使用“案例”:

declare @i1 int=5
select case when @i1 < 0 then 0 
when @i1=> 1 then 1 
end  results1
于 2015-03-18T13:41:42.063 回答