如果没有插入记录,如何在SQL Server2008中使用group by函数将记录的总和显示为零而不是null [英] how to show sum of records to zero in place of null using group by function in sql server2008 if there is no record inserted
问题描述
我为此尝试了以下脚本:
-用于创建表的脚本
创建表TestSum(
SomeValue INT,
TestId INT,
TestName VARCHAR(20)
);
-显示零代替空白,但不显示空白
SELECT ISNULL(Sum(SomeValue),0)从TestSum组按somevalue,testid;
有人可以帮我吗,紧急...
提前谢谢.
--Deepika
i trid below script for this:
-- script for creating table
Create table TestSum (
SomeValue INT,
TestId INT,
TestName VARCHAR(20)
);
-- to display zero in place of blank but not working its giving blank
SELECT ISNULL(Sum(SomeValue),0) FROM TestSum group by somevalue,testid;
can anybody help me, its urgent...
Thanks in advance.
--Deepika
推荐答案
对我有用:
It works for me:
SELECT ISNULL(Sum(iD),0) FROM [myDatabase].[dbo].[myTable] WHERE iD=4
返回一行,其中包含0,因为没有ID为"4"的记录.如果删除WHERE子句,它将返回一行包含"3"的行,因为我的表有两条记录,其iD为"1"和"2"
您如何使用它?
Returns a single row, containing 0 as there are no records with an id of "4". If I remove the WHERE clause, it returns a single row, containing "3" as my table has two records, with iDs "1" and "2"
How are you using this?
这篇关于如果没有插入记录,如何在SQL Server2008中使用group by函数将记录的总和显示为零而不是null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!