sql server查询一条数据如果这条数据不存在怎么给这一行数据赋上0

如题所述

select top 1 I1,sum(RunTime) as RunTime
from
(select I1,0 from PDS_RunDaily
union all
select I1,sum(RunTime) as RunTime from PDS_RunDaily where I1 = '104' and MillID=10 and InsertTime between '2013-11-23' and '2013-11-24' group by I1) t
group by I1
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-11-26
select count(1) from  biao where id=12345

追问

这样不行哦 如过我只查询2列的话就不行

select top 1 I1,sum(RunTime) as RunTime from PDS_RunDaily where I1 = '104' and MillID=10 and InsertTime
between '2013-11-23' and '2013-11-24' group by I1
我这sql 语句是这样的

追答需要判断 检测!
if exists (select top 1 I1,sum(RunTime) as RunTime from PDS_RunDaily where I1 = '104' and MillID=10 and InsertTime 
between '2013-11-23' and '2013-11-24' )
begin
    select top 1 I1,sum(RunTime) as RunTime from PDS_RunDaily where I1 = '104' and MillID=10 and InsertTime 
between '2013-11-23' and '2013-11-24' group by I1
end
else
begin
    select I1 = 0,RunTime = 0
end

追问

大神

本回答被提问者和网友采纳
第2个回答  2013-11-26
select nvl(id,0) from table
第3个回答  2015-09-25
case when column is null then 0 end
或者
nvl(column,0)
第4个回答  2013-11-26
用 isnull

select isnull(productid,0) as pid form tablename where typeid=0