我在SQL中做了一些复杂的数学运算:
Select CAST((CAST(((COUNT(CASE WHEN CONVERT(varchar(5), ReportDate, 108) <= '12:59' AND CLOSE_BY <> '' AND CONVERT(varchar(5), closedate, 108) <= '15:00' THEN CLOSE_BY ELSE NULL END) * 100)/nullif(COUNT(CASE WHEN CONVERT(varchar(5), ReportDate, 108) <= '12:59' THEN ReportDate ELSE NULL END),0)) as decimal(18,2))+
CAST(((COUNT(CASE WHEN CONVERT(varchar(5), ReportDate, 108) between '13:00' and '14:59' AND CLOSE_BY <> '' AND CONVERT(varchar(5), closedate, 108) <= '17:00' THEN CLOSE_BY ELSE NULL END) * 100)/nullif(COUNT(CASE WHEN CONVERT(varchar(5), ReportDate, 108) between '13:00' and '14:59' THEN ReportDate ELSE NULL END),0)) as decimal(18,2))+
CAST(((COUNT(CASE WHEN CONVERT(varchar(5), ReportDate, 108) between '15:00' and '16:59' AND CLOSE_BY <> '' AND CONVERT(varchar(5), closedate, 108) <= '19:00' THEN CLOSE_BY ELSE NULL END) * 100)/nullif(COUNT(CASE WHEN CONVERT(varchar(5), ReportDate, 108) between '15:00' and '16:59' THEN ReportDate ELSE NULL END),0)) as decimal(18,2))+
CAST(((COUNT(CASE WHEN CONVERT(varchar(5), ReportDate, 108) between '17:00' and '18:59' AND CLOSE_BY <> '' AND CONVERT(varchar(5), closedate, 108) <= '21:00' THEN CLOSE_BY ELSE NULL END) * 100)/nullif(COUNT(CASE WHEN CONVERT(varchar(5), ReportDate, 108) between '17:00' and '18:59' THEN ReportDate ELSE NULL END),0)) as decimal(18,2))+
CAST(((COUNT(CASE WHEN CONVERT(varchar(5), ReportDate, 108) >= '19:00' AND CLOSE_BY <> '' AND CONVERT(varchar(5), closedate, 108) <= '23:59' THEN CLOSE_BY ELSE NULL END) * 100)/nullif(COUNT(CASE WHEN CONVERT(varchar(5), ReportDate, 108) >= '18:00' THEN ReportDate ELSE NULL END),0)) as decimal(18,2))) / 5 as decimal(18,2)) as Avg,
getdate() as generatedate FROM WO
WHERE contractor is not null and
contractor <> 'SYSTEM DEFAULT' and LEFT(SCA,1)<>'L' and LEFT(SCA,1)<>' '
and SCH_DATE between ( SELECT CONVERT(DATE , GETDATE() , 103)) and ( SELECT CONVERT(DATE , GETDATE() , 103)) GROUP BY CONTRACTOR,Branch order by CONTRACTOR asc
某些值具有Null值,因此添加将不起作用,并且它变为null。我想仍然处理添加而不使null为0,是否可能?
答案 0 :(得分:1)
NULL
上的算术运算返回NULL,因此如果要返回值并忽略NULL
,则必须将NULL
替换为0.使用ISNULL
标准SQL函数(example: ISNULL(<expression>, 0)
)可以用0替换NULL。