Bha*_*hra 158 sql sql-server
我已经开发了一个查询,并在前三列的结果中得到了NULL.我该如何更换0?
Select c.rundate,
sum(case when c.runstatus = 'Succeeded' then 1 end) as Succeeded,
sum(case when c.runstatus = 'Failed' then 1 end) as Failed,
sum(case when c.runstatus = 'Cancelled' then 1 end) as Cancelled,
count(*) as Totalrun from
( Select a.name,case when b.run_status=0 Then 'Failed' when b.run_status=1 Then 'Succeeded'
when b.run_status=2 Then 'Retry' Else 'Cancelled' End as Runstatus,
---cast(run_date as datetime)
cast(substring(convert(varchar(8),run_date),1,4)+'/'+substring(convert(varchar(8),run_date),5,2)+'/' +substring(convert(varchar(8),run_date),7,2) as Datetime) as RunDate
from msdb.dbo.sysjobs as a(nolock) inner join msdb.dbo.sysjobhistory as b(nolock)
on a.job_id=b.job_id
where a.name='AI'
and b.step_id=0) as c
group by
c.rundate
Run Code Online (Sandbox Code Playgroud)
pha*_*unk 344
如果要将nullelse列替换为其他内容,请使用IsNull.
SELECT ISNULL(myColumn, 0 ) FROM myTable
Run Code Online (Sandbox Code Playgroud)
如果它首先为null,则会在myColumn中输入0.
Moj*_*ian 76
您可以使用这两种方法,但存在差异:
SELECT ISNULL(col1, 0 ) FROM table1
SELECT COALESCE(col1, 0 ) FROM table1
Run Code Online (Sandbox Code Playgroud)
比较COALESCE()和ISNULL():
ISNULL函数和COALESCE表达式具有相似的目的,但行为可能不同.
因为ISNULL是一个函数,所以它只被评估一次.如上所述,可以多次评估COALESCE表达式的输入值.
结果表达式的数据类型确定是不同的.ISNULL使用第一个参数的数据类型,COALESCE遵循CASE表达式规则并返回具有最高优先级的值的数据类型.
对于ISNULL和COALESCE,结果表达式的可空性是不同的.ISNULL返回值始终被视为NOT NULLable(假设返回值是非可空的),而具有非null参数的COALESCE被视为NULL.因此,表达式ISNULL(NULL,1)和COALESCE(NULL,1)虽然等效,但具有不同的可为空性值.如果您在计算列中使用这些表达式,创建键约束或使标量UDF的返回值具有确定性,以便可以对其进行索引(如以下示例所示),则会产生差异.
- 此语句失败,因为PRIMARY KEY不能接受NULL值 - 并且col2的COALESCE表达式的可为空性 - 评估为NULL.
CREATE TABLE #Demo
(
col1 integer NULL,
col2 AS COALESCE(col1, 0) PRIMARY KEY,
col3 AS ISNULL(col1, 0)
);
Run Code Online (Sandbox Code Playgroud)
- 此语句成功,因为 - ISNULL函数的可为空性评估AS NOT NULL.
CREATE TABLE #Demo
(
col1 integer NULL,
col2 AS COALESCE(col1, 0),
col3 AS ISNULL(col1, 0) PRIMARY KEY
);
Run Code Online (Sandbox Code Playgroud)
ISNULL和COALESCE的验证也不同.例如,ISNULL的NULL值转换为int,而对于COALESCE,您必须提供数据类型.
ISNULL只接受2个参数,而COALESCE接受可变数量的参数.
小智 21
用coalesce:
coalesce(column_name,0)
Run Code Online (Sandbox Code Playgroud)
虽然,在总结when condition then 1,你可以很容易地改变sum,以count-例如:
count(case when c.runstatus = 'Succeeded' then 1 end) as Succeeded,
Run Code Online (Sandbox Code Playgroud)
(Count(null)返回0,同时sum(null)返回null.)
当你说前三列时,你的意思是你的SUM列吗?如果是,请添加ELSE 0到您的对CASE帐单中.在SUM一个的NULL值NULL.
sum(case when c.runstatus = 'Succeeded' then 1 else 0 end) as Succeeded,
sum(case when c.runstatus = 'Failed' then 1 else 0 end) as Failed,
sum(case when c.runstatus = 'Cancelled' then 1 else 0 end) as Cancelled,
Run Code Online (Sandbox Code Playgroud)
使用COALESCE,返回第一个非空值,例如
SELECT COALESCE(sum(case when c.runstatus = 'Succeeded' then 1 end), 0) as Succeeded
Run Code Online (Sandbox Code Playgroud)
如果返回为,则将Succeeded设置为0 NULL.
一个简单的方法是
UPDATE tbl_name SET fild_name = value WHERE fild_name IS NULL
Run Code Online (Sandbox Code Playgroud)
如果您使用 Presto、AWS Athena 等,则没有 ISNULL() 函数。相反,使用:
SELECT COALESCE(myColumn, 0 ) FROM myTable
Run Code Online (Sandbox Code Playgroud)