Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 无法对列值求和_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

Sql 无法对列值求和

Sql 无法对列值求和,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我使用下面的查询对我的列值求和 查询: select '$ ' + REPLACE( CONVERT(VARCHAR(32),cast(round(isnull(sum([Share_Invest]),0),0)as MONEY),1), '.00', '') [Investment], '$ ' + REPLACE( CONVERT(VARCHAR(32),cast(round(isnull(sum([Profit_Share]),0),0)as MONEY),1), '.00',

我使用下面的查询对我的列值求和

查询:

select 
'$ ' + REPLACE( CONVERT(VARCHAR(32),cast(round(isnull(sum([Share_Invest]),0),0)as     MONEY),1), '.00', '') [Investment], 
'$ ' + REPLACE( CONVERT(VARCHAR(32),cast(round(isnull(sum([Profit_Share]),0),0)as MONEY),1), '.00', '') [Profit Amount],
[Variance] = '$ ' + REPLACE( CONVERT(VARCHAR(32),cast(round(isnull(sum([Share_Invest]-[Profit_Share]),0),0)as MONEY),1), '.00', '') 
from Finance

第一列显示了它的精细和总和。但是我的Profit_Share列也有空值和数据,这并不是列值的总和


谁能告诉我哪里出了问题。

你的问题没有问题,
但如果[股份投资]或[利润股份]的价值是小数($50.05,$10.10),你可能会遇到麻烦

假设@finance表是您的财务表:

 declare @finance table
(
 id int identity,
 [Share_Invest] money,
 [Profit_Share] money
)


insert into @finance
select '50.05', '10.00' union all
select '20.05', '5.00' union all
select null, '5.00' union all
select null, null union all
select '10.00', null union all
select null, null

-- your query
select 
'$ ' + REPLACE( CONVERT(VARCHAR(5),cast(round(isnull(sum([Share_Invest]),0),0)as     MONEY),1), '.00', '') [Investment], 
'$ ' + REPLACE( CONVERT(VARCHAR(5),cast(round(isnull(sum([Profit_Share]),0),0)as MONEY),1), '.00', '') [Profit Amount],
[Variance] = '$ ' + REPLACE( CONVERT(VARCHAR(5),cast(round(isnull(sum([Share_Invest]-[Profit_Share]),0),0)as MONEY),1), '.00', '') 
from @Finance

/*
Investment Profit Amount Variance
---------- ------------- ---------
$ 80       $ 20          $ 60
*/

-- modified query
select
'$ ' + cast(sum(isnull([Share_invest],0)) as nvarchar(5))  as [Investment]
,'$ ' + cast(sum(isnull([Profit_Share],0)) as nvarchar(5))  as [Profit Amount]
, '$ ' + cast(sum(isnull([Share_invest],0) - isnull([Profit_Share],0)) as nvarchar(max)) as [Variance]
from @finance

/*
Investment Profit Amount Variance
---------- ------------- ---------
$ 80.10    $ 20.00       $ 60.10
*/

我删除了cast(作为money),并取整了

您使用的是什么dbms?sql语法中没有错误,您能否提供您想要的示例数据和结果
null
具有传染性,任何涉及sql
null
(数学、连接、比较等)的操作都会变成
null
本身。“我的利润分享列也有null值和数据”。你所说的“数据”是什么意思?文本数据?您可能希望以另一种方式进行,而不是
isnull(sum(
),而是
sum(isnull(