如何按功能一起使用Sum,Cast和Partition?

时间:2019-03-03 05:36:58

标签: sql sql-server ssms

如何通过函数一起使用Sum,Cast和Partition?

我收到错误消息。

  

将nvarchar值'693.41'转换为数据时转换失败   输入int。”

我尝试过

SUM(CAST([total price] AS INT)) OVER (PARTITION BY (ProjectType)) as TotalPriceV2

完整查询在下面。

SELECT 
    ID,
    [Project Manager], 
    Job#, 
    [Date], 
    [Job Type],
    first_value([Job Name]) OVER (PARTITION BY value_partition ORDER BY ID) CustomerGroup, Value_Partition 
    Customer,
    [Sales Rep1],
    DeliveryType,
    ProjectType,
    [Item Price],
    [Service Price],
    [Total Price],
    SUM(CAST([total price] AS INT)) OVER (PARTITION BY (ProjectType)) as TotalPriceV2
FROM (
    SELECT 
    ID,
    [Project Manager], Job#, [Date], [Job Type], [Job Name],Customer,[Sales Rep1],DeliveryType,ProjectType,[Item Price],[Service Price],[Total Price],
    SUM(CASE WHEN [JOB NAME] IS NULL THEN 0 ELSE 1 END) OVER (ORDER BY ID) AS Value_Partition   
    FROM Testing2
    WHERE 
    [Date] IS NOT NULL AND 
    ([Date]  NOT LIKE '0' OR JOB# NOT LIKE '0' OR [JOB TYPE] NOT LIKE '0')
    AND [Project Manager] NOT LIKE 'ITEM / SERVICE'
) AS X

5 个答案:

答案 0 :(得分:0)

为什么不在内部查询中使用小数(16,9)而不是int来强制转换

SELECT 
    ID,
    [Project Manager], 
    Job#, 
    [Date], 
    [Job Type],
    first_value([Job Name]) OVER (PARTITION BY value_partition ORDER BY ID) CustomerGroup, Value_Partition 
    Customer,
    [Sales Rep1],
    DeliveryType,
    ProjectType,
    [Item Price],
    [Service Price],
    [Total Price],
    SUM([total price]) OVER (PARTITION BY (ProjectType)) as TotalPriceV2
FROM (
    SELECT 
    ID,
    [Project Manager], Job#, [Date], [Job Type], [Job Name],Customer,[Sales Rep1],DeliveryType,ProjectType,[Item Price],[Service Price],cast([Total Price] as decimal(16,9)) as [Total Price],
    SUM(CASE WHEN [JOB NAME] IS NULL THEN 0 ELSE 1 END) OVER (ORDER BY ID) AS Value_Partition   
    FROM Testing2
    WHERE 
    [Date] IS NOT NULL AND 
    ([Date]  NOT LIKE '0' OR JOB# NOT LIKE '0' OR [JOB TYPE] NOT LIKE '0')
    AND [Project Manager] NOT LIKE 'ITEM / SERVICE'
) AS X

答案 1 :(得分:0)

如果您将字符串和数字组合作为列值,并且想要对数字求和。对于SQL Server 2012+,您可以使用TRY_CASTTRY_CONVERT来避免任何转换错误。

TRY_CAST (Transact-SQL)

在您的方案中,您没有使用正确的数据类型进行转换,所以您有693.41之类的值无法转换为INT,而应使用DECIMAL数据类型。

还有一个建议,而不是使用NOT LIKE,最好像下面这样在SQL Server中使用<>

更改

 ([Date]  NOT LIKE '0' OR JOB# NOT LIKE '0' OR [JOB TYPE] NOT LIKE '0')

 ([Date] <> '0' OR JOB# <> '0' OR [JOB TYPE] <> '0')

答案 2 :(得分:0)

整数将没有十进制值,并且将引发错误。

如果使用此选项,则类似于您的示例。

Declare @val nvarchar(10) = '10.24' 

select   cast (@val as int) 

我收到以下错误。

Msg 245, Level 16, State 1, Line 3
Conversion failed when converting the nvarchar value '10.24' to data type int.

现在要避免此错误,我可以将数字四舍五入到小数点后0位并转换为Int。另外,如果您使用的是SQL Server 2012或更高版本,我建议使用Try_Cast避免出现任何实际nvarchar值的情况(它将将其转换为null)。

 Declare @val nvarchar(10) = '10.24' 

select   Try_cast (round(@val,0) as int)   as valuen

输出,我认为这将在您的sum函数中起作用。

valuen
10

答案 3 :(得分:0)

一种可能的解决方案是首先将nvarchar值转换为十进制,然后转换为整数:

CAST(CAST([total price] AS FLOAT) AS INT)

但是,以这种方式,如果对您来说很重要,小数部分就会丢失。

答案 4 :(得分:0)

首先,确定有问题的行。您要转换为int,所以:

select price
from Testing2
where try_convert(int, price) is null and price is not null;

至少某些值将带有小数点(如您的示例所示)。要查看这是否是唯一的问题,我建议接下来将其转换为小数:

select price
from Testing2
where try_convert(decimal(20, 4), price) is null and price is not null;

如果此命令不返回任何内容,则表示您已设置。如果没有,您将需要弄清楚如何解决这些异常。

然后,我将使用小数而不是浮点数来表示计算:

SUM(TRY_CAST([total price] AS DECIMAL(20, 4))) OVER (PARTITION BY (ProjectType)) as TotalPriceV2

通常应使用定点值而不是近似浮点值来表示货币金额。浮点值的问题在于信息可能会丢失。考虑带有real的示例:

select cast(1000000 as real) + cast(0.01 as real),
       cast(1000000 as decimal(20, 4)) + cast(0.01 as decimal(20, 4))