将表达式转换为数据类型int的算术溢出错误

时间:2012-07-06 18:16:52

标签: sql

我正在将数据选择到临时表中,然后查询该表以获取报告的其他值。下面是生成错误的代码,如果它有任何区别,临时表中大约有3600万条记录。它说错误发生在第一个查询的第一列。

SELECT SUBSCRIPTION_ALIAS,
       'Count' = 0,
       COUNT(SUBSCRIPTION_ALIAS) AS MonthChange
FROM #Temp1 A WHERE DATEDIFF(day,JOIN_DTM,'01/01/2012') < 31    
GROUP BY SUBSCRIPTION_ALIAS

UNION

SELECT B.SUBSCRIPTION_ALIAS,
       COUNT(B.SBSCRPTN_MBR_KEY) AS [Count],
       'MonthChange' = 0
FROM #Temp1 B JOIN #Temp1 A ON B.SUBSCRIPTION_ALIAS = A.SUBSCRIPTION_ALIAS
GROUP BY B.SUBSCRIPTION_ALIAS

DROP TABLE #Temp1

这是临时表的声明(很难看)。错误消息是此主题的标题。

SELECT
  bi_communications.F_EML_RCPNT.SBSCRPTN_LIST_KEY,
  bi_communications.F_EML_RCPNT.SBSCRPTN_MBR_KEY,
  bi_communications.D_SBSCRPTN_LIST.SBSCRPTN_LIST_NM,
  bi_communications.F_EML_RCPNT.DLRY_DT_KEY,
  bi_communications.D_DT.DT_DT,

  CASE
WHEN D_SBSCRPTN_LIST.SBSCRPTN_LIST_NM = 'active-offers' THEN 'Special Offers'
WHEN D_SBSCRPTN_LIST.SBSCRPTN_LIST_NM = 'reserveamerica-offers' THEN 'RA Special Offers'
WHEN D_SBSCRPTN_LIST.SBSCRPTN_LIST_NM in
   ('active-team-sports-insider','active-tennis','active-women-news', ... (omitting excess)
   ) THEN 'Content'
    WHEN D_SBSCRPTN_LIST.SBSCRPTN_LIST_NM = 'local-events' THEN 'Local Events'
    ELSE 'Blank'
  END AS SUBSCRIPTION_ALIAS,
  bi_communications.D_SBSCRPTN_MBR.JOIN_DTM
INTO #Temp1     
FROM bi_communications.F_EML_RCPNT
    JOIN
     bi_communications.D_SBSCRPTN_LIST ON bi_communications.F_EML_RCPNT.SBSCRPTN_LIST_KEY = bi_communications.D_SBSCRPTN_LIST.SBSCRPTN_LIST_KEY
JOIN bi_communications.D_DT ON bi_communications.F_EML_RCPNT.DLRY_DT_KEY = bi_communications.D_DT.DT_KEY
    JOIN bi_communications.D_SBSCRPTN_MBR ON bi_communications.F_EML_RCPNT.SBSCRPTN_MBR_KEY = bi_communications.D_SBSCRPTN_MBR.SBSCRPTN_MBR_KEY
WHERE
  bi_communications.F_EML_RCPNT.SBSCRPTN_LIST_KEY  IN 
  ( 180740437, 180741017, 180740482, 180740438, ... )
AND bi_communications.D_DT.DT_DT BETWEEN  '01/01/2011 00:00:00' AND '01/01/2012 00:00:00'

2 个答案:

答案 0 :(得分:1)

嗯,订阅别名在一个包含3400万行的表中有5个左右的不同值。好吧,其中一个将至少有600万行。自连接将在此上产生至少36万亿行,这很快就会在第二个查询中产生算术溢出。

你真的是这个意思吗?

SELECT B.SUBSCRIPTION_ALIAS, COUNT(B.SBSCRPTN_MBR_KEY) AS [Count],'MonthChange' = 0
FROM #Temp1 B JOIN #Temp1 A
     ON B.SUBSCRIPTION_ALIAS = A.SUBSCRIPTION_ALIAS
GROUP BY B.SUBSCRIPTION_ALIAS

这基本上是对计数的行数进行平方。我猜这个错误是在第二个子查询中;该位置未正确报告。

答案 1 :(得分:0)

你的JOIN_DTM是否有可能无效的日期(超出范围或只是null)然后DATEDIFF运行onit,将其转换为int并导致错误?