简体   繁体   English

将 varchar 转换为数据类型 numeric CASE 语句的算术溢出错误

[英]Arithmetic overflow error converting varchar to data type numeric CASE statement

I am trying to write a query that returns an "Estimated Annual Value", and for this, I am using a Case statement.我正在尝试编写一个返回“估计年值”的查询,为此,我使用了 Case 语句。 There are two inner joins involved in the query.查询中涉及两个内部联接。

So, the query and gives me result when I am running this piece:因此,当我运行这件作品时,查询并给出了结果:

select Users.Id, Name, PhoneNumber, Email, Currency, count(*) as TotalOrders, sum(TotalCustomerAmount) as TotalOrderValue, avg(TotalCustomerAmount) as AverageOrderValue, TsCreate as RegistrationDate, max(TsOrderPlaced) as LastOrderDate, min(TsOrderPlaced) as FirstOrderDate, 
CASE 
    When PromotionsEnabled = 0 then 'Y'
    When PromotionsEnabled = 1 then 'n'
    else 'undefined'
end as Promotions,
/*CASE
    When ((DATEDIFF(day, max(TsOrderPlaced), min(TsOrderPlaced)) >= 6) AND (count(*) > 3)) then  ((sum(TotalCustomerAmount)/(DATEDIFF(day, max(TsOrderPlaced), min(TsOrderPlaced))))*365) 
    Else '-'
end as EstimatedAnnualValue*/
from AspNetUsers with (nolock)
    inner join Orders with (nolock) on Orders.UserId = AspNetUsers.Id and Orders.WhiteLabelConfigId = @WhiteLabelConfigId
                                    and Orders.OrderState in (2,3,4,12)     
    inner join UserWhiteLabelConfigs with (nolock) on UserWhiteLabelConfigs.UserId = AspNetUsers.Id and Orders.WhiteLabelConfigId = @WhiteLabelConfigId
where AspNetUsers.Discriminator = 'ApplicationUser'
group by Users.Id, Name, Number, Currency, Email, TsCreate, PromotionsEnabled

But the problem comes when I am running this with the second CSAE statement, is it because I cannot use the aggregate function before GROUP BY?但是当我使用第二个 CSAE 语句运行它时出现问题,是因为我不能在 GROUP BY 之前使用聚合 function 吗? I am also thinking of using a Subquery Looking fr some help!!我也在考虑使用子查询寻找一些帮助!

You need to use aggregation functions.您需要使用聚合函数。 For instance, if you want 'Y' only when all values are 0 or NULL :例如,如果您仅在所有值为0NULL时才需要'Y'

(case when max(PromotionsEnabled) = 0 then 'Y'
      when max(PromotionsEnabled) = 1 then 'N'
      else 'undefined'
 end) as Promotions,

I'm not sure if this is the logic you want (because that detail is not in the question).我不确定这是否是您想要的逻辑(因为该细节不在问题中)。 However, this shows that you can use aggregation functions in a case expression .但是,这表明您可以在case表达式中使用聚合函数。

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

相关问题 算术溢出错误将varchar转换为数据类型numeric -error - Arithmetic overflow error converting varchar to data type numeric -error 错误:将数字转换为数据类型varchar的算术溢出错误 - Error : Arithmetic overflow error converting numeric to data type varchar 错误:将varchar转换为数值类型的算术溢出错误 - Error: Arithmetic overflow error converting varchar to data type numeric 将数字转换为数据类型varchar的算术溢出错误 - Arithmetic overflow error converting numeric to data type varchar 将varchar转换为数值类型-VBA的算术溢出错误 - Arithmetic overflow error converting varchar to data type numeric -VBA 在存储过程中将varchar转换为数值类型的算术溢出错误 - Arithmetic overflow error converting varchar to data type numeric in stored procedure 将varchar转换为数值类型的算术溢出错误 - Arithmetic overflow error converting varchar to data type numeric 将 varchar 转换为数据类型 numeric 的算术溢出错误 - Arithmetic overflow error converting varchar to data type numeric 将varchar转换为数值数据类型的算术溢出错误? - Arithmetic overflow error converting varchar to data type numeric? 在SQL Server中将varchar转换为数值类型的算术溢出错误 - Arithmetic overflow error converting varchar to data type numeric in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM