【问题标题】:SQL Server: Issues with data typeSQL Server:数据类型问题
【发布时间】:2015-06-23 10:26:52
【问题描述】:
SELECT pa.[type], (SUM(pa.Actions_Logged)/SUM(pi.Impressions_Served)) AS ActionRates  
from Performance_Actions pa
INNER JOIN Performance_Impressions pi
ON pa.Alternative = Pi.Alternative
GROUP BY pa.[type];

以上查询产生错误:

Lookup Error - SQL Server Database Error: Arithmetic overflow error converting expression to data type int.

pa.Actions_Loggedpi.Impressions_Served 都是整数,结果是小数。是这个问题吗?

表格是 圆周率:

Date       Alternative  Impressions_Served
05/04/2015      x           544432
05/04/2015      x           545990
18/04/2015      y           343325
06/04/2015      z           591316
06/04/2015      y                2

帕:

Date       Alternative  Actions_Logged  Type
05/04/2015     x            33         landing
01/04/2015     y             3         conversion
06/04/2015     k             8         landing
01/04/2015     x             3         conversion
18/04/2015     y             3         landing

【问题讨论】:

  • Actions_LoggedImpressions_Served 的数据类型是什么
  • @AmeyaDeshpande 两者都是 int。

标签: sql sql-server toad


【解决方案1】:

SUM 中的表达式类型决定了返回类型。

尝试以下方法:

SELECT pa.[type], (SUM(CAST(pa.Actions_Logged as BIGINT ))/SUM(CAST(pi.Impressions_Served as BIGINT ))) AS ActionRates  
from Performance_Actions pa
INNER JOIN Performance_Impressions pi
ON pa.Alternative = Pi.Alternative
GROUP BY pa.[type];

【讨论】:

  • 现在可以使用 谢谢。如果我需要将结果设为百分比,那么我可以进一步修改它吗?
【解决方案2】:

试试这个查询

SELECT pa.[type], (isnull( SUM(pa.Actions_Logged),0)/isnull(SUM(pi.Impressions_Served),0)) AS ActionRates  
from Performance_Actions pa
INNER JOIN Performance_Impressions pi
ON pa.Alternative = Pi.Alternative
GROUP BY pa.[type];

【讨论】:

    猜你喜欢
    • 2016-05-22
    • 2011-05-20
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多