这是我尝试的代码
SELECT
case when InsertedRows is null then 0 else InsertedRows end InsertedRows
,case when FailedRows is null then 0 else FailedRows end FailedRows
,case when UpdatedRows is null then 0 else UpdatedRows end UpdatedRows
,InsertedRows + UpdatedRows + FailedRows as tot
FROM PATS.ImportLog
WHERE CreatedBy='suvaneeth'
AND ISNULL(CompletedYN,0) = 0
AND CAST(CreatedDate AS date) >= CAST(GETDATE() AS date)
我得到tot
的结果为NULL
99 0 0 NULL
我希望结果是99
答案 0 :(得分:2)
select
case when InsertedRows is null then 0 else InsertedRows end InsertedRows
,case when FailedRows is null then 0 else FailedRows end FailedRows
,case when UpdatedRows is null then 0 else UpdatedRows end UpdatedRows
,COALESCE(InsertedRows,0) + COALESCE(UpdatedRows,0) + COALESCE(FailedRows,0) as tot
from PATS.ImportLog
WHERE CreatedBy='suvaneeth' AND ISNULL(CompletedYN,0)=0 AND CAST(CreatedDate AS date)>=CAST(GETDATE() AS date)
答案 1 :(得分:1)
您可以尝试使用ISNULL(insertedrows, 0)
或COALESCE(insertedrows, 0)
代替CASE
答案 2 :(得分:0)
以这种方式尝试。
select
Coalesce(InsertedRows, 0) InsertedRows
,Coalesce(FailedRows, 0) FailedRows
,Coalesce(UpdatedRows, 0) UpdatedRows
,Coalesce(InsertedRows, 0) + Coalesce(FailedRows, 0)+ Coalesce(UpdatedRows, 0) as tot
from PATS.ImportLog
WHERE CreatedBy='suvaneeth' AND ISNULL(CompletedYN,0)=0 AND CAST(CreatedDate AS date)>=CAST(GETDATE() AS date)
答案 3 :(得分:0)
表达式:
SELECT ..., InsertedRows + UpdatedRows + FailedRows
引用FROM子句中的列,而不引用SELECT子句中的列名。您需要重复CASE语句,或者更好的方法是使用COALESCE,它比您使用的case语句短:
SELECT COALESCE(InsertedRows, 0) InsertedRows
, COALESCE(FailedRows, 0) FailedRows
, COALESCE(UpdatedRows, 0) UpdatedRows
, COALESCE(InsertedRows, 0) + COALESCE(UpdatedRows, 0) + COALESCE(FailedRows, 0) AS tot
FROM PATS.ImportLog
WHERE CreatedBy = 'suvaneeth'