select 计数案例计算
select count case calculation
我有一个 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
有人能帮忙吗?
您可以这样使用 'case':
declare @i1 int=5
select case when @i1 < 0 then 0
when @i1=> 1 then 1
end results1
只需使用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
我有一个 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
有人能帮忙吗?
您可以这样使用 'case':
declare @i1 int=5
select case when @i1 < 0 then 0
when @i1=> 1 then 1
end results1
只需使用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