简体   繁体   English

SQL Server:包含字母数字值的列的总和

[英]SQL Server : SUM of column with alphanumeric values

I want to get the sum of a column that is alphanumeric.我想得到一列字母数字的总和。 I want to add numeric values and return column values if not numeric.如果不是数字,我想添加数值并返回列值。 What I did is a added a CASE WHEN that looks like this我所做的是添加一个CASE WHEN看起来像这样

CASE 
   WHEN intAllocatedResourceperDivision NOT IN ('CL', 'HS', 'HV', 'ML', 'SL', 'VL', 'HC', 'S', '*') 
      THEN CAST(SUM(ISNULL(CAST(intAllocatedResourceperDivision AS DECIMAL), 0.00)) AS NVARCHAR(250)) 
      ELSE intAllocatedResourceperDivision 
END intAllocatedResourceperDivision

So I assume that all numeric values will be added and if values is in ('CL', 'HS', 'HV', 'ML', 'SL', 'VL', 'HC', 'S', '*') it will be returned as is.所以我假设所有数值都将被添加,如果值在('CL', 'HS', 'HV', 'ML', 'SL', 'VL', 'HC', 'S', '*')它将按原样返回。

But I'm getting但我得到

Error converting data type nvarchar to numeric.将数据类型 nvarchar 转换为数字时出错。

Looks like your SUM aggregation is out of place.看起来您的 SUM 聚合不合适。 You only sum if the condition in the case statement is true.您只在 case 语句中的条件为真时求和。 Try this:尝试这个:

SUM(case when intAllocatedResourceperDivision NOT IN ('CL','HS','HV','ML','SL','VL','HC','S','*') THEN intAllocatedResourceperDivision else 0 end)

In case you don't know the exact potential combination of non numeric values, you can use the ISNUMERIC function (assuming you're using SQL Server) to test whether or not the value is a number and assign a 0 if it's not, aggregating the final result.如果您不知道非数值的确切潜在组合,您可以使用ISNUMERIC函数(假设您使用的是 SQL Server)来测试该值是否为数字,如果不是则分配 0,聚合最终结果。

SUM(case when ISNUMERIC(intAllocatedResourceperDivision) = 1 then intAllocatedResourceperDivision else 0 end)

To aggregate the numerical values and also keep non-numerical, you can use a union query as such:要聚合数值并保持非数值,您可以使用联合查询,如下所示:

select
    cast(SUM(cast(intAllocatedResourceperDivision as decimal(18,2))) as varchar)
from
    YOUR_TABLE
where
    ISNUMERIC(intAllocatedResourceperDivision) = 1

UNION ALL

select
    intAllocatedResourceperDivision
from
    YOUR_TABLE
where
    ISNUMERIC(intAllocatedResourceperDivision) = 0

This looks like SQL Server syntax.这看起来像 SQL Server 语法。 I would recommend using TRY_CONVERT() :我建议使用TRY_CONVERT()

TRY_CONVERT(DECIMAL, intAllocatedResourceperDivision) as intAllocatedResourceperDivision

Try this:尝试这个:

select 'CL' as intAllocatedResourceperDivision into #tmp
union select 'HS'union select 'HV'union select 'ML'union select 'SL'union select 'VL'union select 'HC'union select 'S'union select '*'union select '1'union select '4'

select CAST(SUM(ISNULL(CAST(intAllocatedResourceperDivision AS DECIMAL),0.00)) AS nvarchar(250)) as intAllocatedResourceperDivision
from #tmp where intAllocatedResourceperDivision NOT IN ('CL','HS','HV','ML','SL','VL','HC','S','*')
union 
select intAllocatedResourceperDivision
from #tmp where intAllocatedResourceperDivision IN ('CL','HS','HV','ML','SL','VL','HC','S','*')

cast your as varchar to match your IN operator values. cast您转换为varchar以匹配您的IN运算符值。

,CASE WHEN cast(intAllocatedResourceperDivision as varchar(2)) 
    NOT IN ('CL','HS','HV','ML','SL','VL','HC','S','*') 
 THEN CAST(SUM(ISNULL(CAST(intAllocatedResourceperDivision AS DECIMAL),0.00)) as nvarchar(250)) 
 ELSE cast(intAllocatedResourceperDivision as nvarchar(250))  END intAllocatedResourceperDivision

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM