在SQL Server查询中转换Varchar的算术错误

时间:2015-02-03 18:15:15

标签: sql sql-server math varchar

我收到错误

  

Msg 8115,Level 16,State 8,Line 2
  算术溢出错误将数字转换为数据类型数字

尝试执行以下SQL时。关于我可以做些什么来纠正这个问题的任何想法?我确信它与VarChar有关,但我不知道我能做些什么来修复它。

SELECT        
   CONVERT(VARCHAR(10), t1.date_transaction, 10) AS summaryDate,      
   t1.qb_merchant_id, t1.store_id, 
   SUM(t1.amount) - ISNULL(SUM(te.TransAmount), 0) + 
       ISNULL(SUM(te.OriginalTransAmount), 0) AS grossSales, 
   SUM(CASE WHEN t1.voided = '1' THEN 0 ELSE t1.amount END) AS netSales,
   (SELECT COUNT(*) AS Expr1
    FROM dbo.sas_transactions AS t3
    WHERE (voided = '1') AND (qb_merchant_id = t1.qb_merchant_id) AND (store_id = t1.store_id) AND (CONVERT(VARCHAR(10), date_transaction, 10) = CONVERT(VARCHAR(10), t1.date_transaction, 10))) AS voids, 
   SUM(ISNULL(te.Commission, CASE WHEN t1.voided = '1' THEN 0 ELSE t1.amount_commission END)) AS totalCommissions, 
   SUM(CASE WHEN t1.voided = '1' THEN 0 ELSE t1.amount_sas END) AS totalShareASale,
   (SELECT COUNT(*) AS Expr1
    FROM dbo.sas_transactions AS t2
    WHERE (transaction_type = 'Sale' OR transaction_type = 'Manual Sale') AND (qb_merchant_id = t1.qb_merchant_id) AND (store_id = t1.store_id) AND (voided <> '1') AND (CONVERT(VARCHAR(10), date_transaction, 10) = CONVERT(VARCHAR(10), t1.date_transaction, 10))) AS numOrders,
   (SELECT COUNT(*) AS Expr1
    FROM dbo.sas_transactions AS t4
    WHERE (transaction_type = 'Lead') AND (qb_merchant_id = t1.qb_merchant_id) AND (store_id = t1.store_id) AND (voided <> '1') AND (CONVERT(VARCHAR(10), date_transaction, 10) = CONVERT(VARCHAR(10), t1.date_transaction, 10))) AS numLeads,
   (SELECT COUNT(DISTINCT user_id) AS Expr1
    FROM dbo.sas_transactions AS t2
    WHERE (transaction_type = 'Sale' OR transaction_type = 'Manual Sale') AND (qb_merchant_id = t1.qb_merchant_id) AND (store_id = t1.store_id) AND (voided <> '1') AND (CONVERT(VARCHAR(10), date_transaction, 10) = CONVERT(VARCHAR(10), t1.date_transaction, 10))) AS numAffiliates
FROM            
    dbo.sas_transactions AS t1 
LEFT OUTER JOIN
    dbo.sas_transaction_edits AS te ON t1.transaction_id =  te.TransID
WHERE        
    (t1.transaction_type <> 'Manual Deposit') 
    AND (t1.transaction_type <> 'Autodeposit')
GROUP BY 
    CONVERT(VARCHAR(10), t1.date_transaction, 10), t1.qb_merchant_id, t1.store_id

我也遇到了同样的错误:

    SELECT        CONVERT(VARCHAR(10), t1.date_transaction, 10) AS summaryDate, t1.qb_merchant_id, t1.store_id, SUM(t1.amount) - ISNULL(SUM(te.TransAmount), 0) + ISNULL(SUM(te.OriginalTransAmount), 0) AS grossSales, 
                     SUM(CASE WHEN t1.voided = '1' THEN 0 ELSE t1.amount END) AS netSales,
                         (SELECT        COUNT(*) AS Expr1
                           FROM            dbo.sas_transactions AS t3
                           WHERE        (voided = '1') AND (qb_merchant_id = t1.qb_merchant_id) AND (store_id = t1.store_id) AND (CONVERT(VARCHAR(10), date_transaction, 10) = CONVERT(VARCHAR(10), t1.date_transaction, 10))) 
                     AS voids, SUM(ISNULL(te.Commission, CASE WHEN t1.voided = '1' THEN 0 ELSE t1.amount_commission END)) AS totalCommissions, SUM(CASE WHEN t1.voided = '1' THEN 0 ELSE t1.amount_sas END) 
                     AS totalShareASale,
                         (SELECT        COUNT(*) AS Expr1
                           FROM            dbo.sas_transactions AS t2
                           WHERE        (transaction_type = 'Sale' OR
                                                     transaction_type = 'Manual Sale') AND (qb_merchant_id = t1.qb_merchant_id) AND (store_id = t1.store_id) AND (voided <> '1') AND (CONVERT(VARCHAR(10), date_transaction, 10) 
                                                     = CONVERT(VARCHAR(10), t1.date_transaction, 10))) AS numOrders,
                         (SELECT        COUNT(*) AS Expr1
                           FROM            dbo.sas_transactions AS t4
                           WHERE        (transaction_type = 'Lead') AND (qb_merchant_id = t1.qb_merchant_id) AND (store_id = t1.store_id) AND (voided <> '1') AND (CONVERT(VARCHAR(10), date_transaction, 10) 
                                                     = CONVERT(VARCHAR(10), t1.date_transaction, 10))) AS numLeads,
                         (SELECT        COUNT(DISTINCT user_id) AS Expr1
                           FROM            dbo.sas_transactions AS t2
                           WHERE        (transaction_type = 'Sale' OR
                                                     transaction_type = 'Manual Sale') AND (qb_merchant_id = t1.qb_merchant_id) AND (store_id = t1.store_id) AND (voided <> '1') AND (CONVERT(VARCHAR(10), date_transaction, 10) 
                                                     = CONVERT(VARCHAR(10), t1.date_transaction, 10))) AS numAffiliates
    FROM            dbo.sas_transactions AS t1 LEFT OUTER JOIN
                     dbo.sas_transaction_edits AS te ON t1.transaction_id = te.TransID
    WHERE        (t1.transaction_type <> 'Manual Deposit') AND (t1.transaction_type <> 'Autodeposit') AND (t1.user_id <> 144212)
    GROUP BY CONVERT(VARCHAR(10), t1.date_transaction, 10), t1.qb_merchant_id, t1.store_id

1 个答案:

答案 0 :(得分:0)

非常确定您的查询可以使用案例表达式简化为类似的内容。现在,通过大大简化的查询,让我们弄清楚您正在运行的问题。我怀疑在你现有的case表达式的某个地方你发生了隐式转换,但很难从这里看出来。

SELECT CONVERT(VARCHAR(10), t1.date_transaction, 10) AS summaryDate
    , t1.qb_merchant_id
    , t1.store_id 
    , Sum(t1.amount) - Isnull(Sum(te.TransAmount), 0) + Isnull(Sum(te.OriginalTransAmount), 0) AS grossSales
    , Sum(CASE WHEN t1.voided = '1' THEN 0 ELSE t1.amount END) AS netSales
    , SUM(case when voided = '1' then 1 end) as voids
    , Sum(Isnull(te.Commission, CASE WHEN t1.voided = '1' THEN 0 ELSE t1.amount_commission END)) AS totalCommissions
    , Sum(CASE WHEN t1.voided = '1' THEN 0 ELSE t1.amount_sas END) AS totalShareASale
    , SUM(case when transaction_type IN ('Sale', 'Manual Sale') and voided <> '1' then 1 end) as numOrders
    , sum(case when transaction_type = 'Lead' then 1 end) as numLeads
    , count(case when transaction_type IN ('Sale', 'Manual Sale') and voided <> '1' then 1 end) as numAffiliates
FROM   dbo.sas_transactions AS t1
LEFT OUTER JOIN dbo.sas_transaction_edits AS te ON t1.transaction_id = te.TransID
WHERE  t1.transaction_type not in ('Manual Deposit', 'Autodeposit')
    AND t1.user_id <> 144212
GROUP BY CONVERT(VARCHAR(10), t1.date_transaction, 10)
    , t1.qb_merchant_id
    , t1.store_id