关于datatable.Compute("sum(je)", "")当datatable为空时怎么处理?

tmxsl1214 2010-10-15 04:56:58
如题.
...全文
1005 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
liubaoen 2011-10-24
  • 打赏
  • 举报
回复
我感觉是你的SQL语句有问题,先在数据库的客户端调试好你的SQL语句,如果有null,一般用ifnull(字段名,0)将null值转化为0,再计算就可以了.
sum()不能应用于含有null值的列.这个问题在微软的官方网站上有,提出的解决办法也是修改查询语句,尽量不要返回null值,不好处理.
beyond_me21 2010-10-16
  • 打赏
  • 举报
回复
搞不定,我试了好多种方法都无法通过sql语句当无数据时返回0,你还是用数据源判断吧
tmxsl1214 2010-10-16
  • 打赏
  • 举报
回复
现在不报错了.但是还是为空不转化为0
beyond_me21 2010-10-16
  • 打赏
  • 举报
回复
select isnull(hdje,0) from
(select round(sum(isnull(hdje,0)),0) as hdje from JLZF_jg where cbdh='TJ01' and bh in (select bh from JLZF_jgT where zfqh <> 0 and zfqh =10) group by left(dh,1)) a

再试一下
tmxsl1214 2010-10-16
  • 打赏
  • 举报
回复
感谢各位的帮助.
select isnull(hdje,0) from (select isnull(round(sum(hdje),0),0) hdje from JLZF_jg where cbdh='TJ01' and bh in (select bh from JLZF_jgT where zfqh <> 0 and zfqh =10) group by left(dh,1))
这个SQL报语法错误.服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: ')' 附近有语法错误。

================================================
if(dt!=buu && dt.Rows.Count>0)
object o=dt.Compute("sum(je)", "je is not null")
我就是不想麻烦因为如果这样的话我要改的地方很多.我希望直接在dt.Compute("sum(je)", "je is not null")
这一句里解决,或者直接在SQL中为空变为0.

谢谢大家,请继续.
tmxsl1214 2010-10-16
  • 打赏
  • 举报
回复
感谢各位的帮助.
select isnull(hdje,0) from (select isnull(round(sum(hdje),0),0) hdje from JLZF_jg where cbdh='TJ01' and bh in (select bh from JLZF_jgT where zfqh <> 0 and zfqh =10) group by left(dh,1))
这个SQL报语法错误.服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: ')' 附近有语法错误。

================================================
if(dt!=buu && dt.Rows.Count>0)
object o=dt.Compute("sum(je)", "je is not null")
我就是不想麻烦因为如果这样的话我要改的地方很多.我希望直接在dt.Compute("sum(je)", "je is not null")
这一句里解决,或者直接在SQL中为空变为0.

谢谢大家,请继续.
tmxsl1214 2010-10-16
  • 打赏
  • 举报
回复
非常感谢,如果没有简单的方法只能用存储过程或视图了.星期一结贴,如果没有简单的方法分都给你.如果有办法我再加60分.
beyond_me21 2010-10-16
  • 打赏
  • 举报
回复

CREATE PROCEDURE usp_GetTotal

AS

DECLARE @counts int --临时记录查询结果有否有记录
DECLARE @sql nvarchar(500)
DECLARE @ParmDefinition nvarchar(500)
SET @sql = N'SELECT @counts=COUNT(*) from from JLZF_jg where cbdh=''TJ01'' and bh in (select bh from JLZF_jgT where zfqh <> 0 and zfqh =10)';
SET @ParmDefinition = N'@counts int OUTPUT';
EXECUTE sp_executesql @sql, @ParmDefinition, @counts OUTPUT;
IF @counts <>0
BEGIN
select round(sum(isnull(hdje,0)),0) as hdje from JLZF_jg where cbdh='TJ01' and bh in (select bh from JLZF_jgT where zfqh <> 0 and zfqh =10) group by left(dh,1)
END
ELSE
BEGIN
SELECT hdje=0
END

GO
beyond_me21 2010-10-16
  • 打赏
  • 举报
回复
我早上帮你试过好几种方法,没办法在没有数据行的条件下返回值,你可以试试用存储过程,判断当没有数据里返回0
tmxsl1214 2010-10-16
  • 打赏
  • 举报
回复
兄弟们,帮帮忙.
wuyq11 2010-10-15
  • 打赏
  • 举报
回复
if(dt!=buu && dt.Rows.Count>0)
object o=dt.Compute("sum(je)", "je is not null")
beyond_me21 2010-10-15
  • 打赏
  • 举报
回复
select isnull(hdje,0) from (select isnull(round(sum(hdje),0),0) hdje from JLZF_jg where cbdh='TJ01' and bh in (select bh from JLZF_jgT where zfqh <> 0 and zfqh =10) group by left(dh,1))
tmxsl1214 2010-10-15
  • 打赏
  • 举报
回复
无法计算。表达式“System.Data.FunctionNode”不是聚合。用楼上的方法报这个错.
datatable.Compute("isnull(sum(je),0)", "") 也报同样的错.
「已注销」 2010-10-15
  • 打赏
  • 举报
回复
datatable.Compute("sum(je)", "je is not null")
tmxsl1214 2010-10-15
  • 打赏
  • 举报
回复
放外面是一样的效果.
beyond_me21 2010-10-15
  • 打赏
  • 举报
回复
select sum(isnull(round(hdje,0),0)) hdje from JLZF_jg where cbdh='TJ01' and bh in (select bh from JLZF_jgT where zfqh <> 0 and zfqh =10) group by left(dh,1)

吧sum放外面
tmxsl1214 2010-10-15
  • 打赏
  • 举报
回复
select isnull(round(sum(hdje),0),0) hdje from JLZF_jg where cbdh='TJ01' and bh in (select bh from JLZF_jgT where zfqh <> 0 and zfqh =10) group by left(dh,1)
这是我的SQL,在没加分组的时候没有符合条件的会给出一个0,但加了分组没有符合条件的时候就是空了.这里有没有办法让isnull()继续起作用?
tmxsl1214 2010-10-15
  • 打赏
  • 举报
回复
这个我也知道,但我想简单点,有没有办法.比如在datatable.Compute("sum(je)", "")这个条件过滤这有没有办法处理?
beyond_me21 2010-10-15
  • 打赏
  • 举报
回复
if (datatable.Rows.Count>0){//你的代码}else{//}

62,042

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧