Pregunta

I'm having an issue in SQL Server I've been trying to debug for quite some time now and I can't figure out where the problem is.

If I run this query…

SELECT 
    CAST(Hotel_Id AS bigint) AS Hotel_Id, 
    Hotel_Name, 
    CAST(Room_Category AS bigint) AS Room_Category, 
    Room_Category_Name, 
    CAST(Room_Type AS bigint) AS Room_Type, 
    Room_Type_Name, 
    Info_URL_Title, 
    Info_URL , 
    MAX(CAST(ISNULL(Price_Excl_VAT, 0) AS bigint)) AS Price_Excl_VAT, 
    CASE 
        WHEN MAX(CAST(ISNULL(Price_Excl_VAT, 0) AS bigint)) = 0 
        THEN 0 ELSE MAX(CAST(ISNULL(Price, 0) AS bigint)) - MAX(CAST(ISNULL(Price_Excl_VAT, 0) AS bigint)) 
    END AS VAT,
    MAX(CAST(ISNULL(Price, 0) AS bigint)) AS Price,
    MAX(CAST(ISNULL(Dep_Amount, 0) AS [bigint])) AS Dep_Amount  
FROM 
    uvw_HotelAllotmentToBook 
WHERE Client_Id = 'CLIENT' AND Project_Id = 'PROJECT' 
    AND Allotment_Date >= '2014-05-11' AND Allotment_Date < '2014-05-14' 
GROUP BY Hotel_Id, Hotel_Name, Room_Category, Room_Category_Name, Room_Type, Room_Type_Name, Info_URL_Title, Info_URL, Dep_Amount
HAVING COUNT(Allotment_Date) >= 3

In for example one project that we have a bit over 4000 records it keeps giving me this error

Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type int.

I've been googling around but can't find a way to fix this, I tried as you can see casting all number fields to big int with no luck.

I need to find the MAX of the prices because the price per night can change.

UPDATE

SELECT Hotel_Id
   ,Hotel_Name
   ,Room_Category AS Room_Category
   ,Room_Category_Name
   ,Room_Type AS Room_Type
   ,Room_Type_Name
   ,Info_URL_Title
   ,Info_URL
   ,COUNT(Allotment_Date)
   ,MAX(CAST(ISNULL(Price_Excl_VAT, 0) AS BIGINT)) AS Price_Excl_VAT
   ,CASE WHEN MAX(CAST(ISNULL(Price_Excl_VAT, 0) AS BIGINT)) = 0 THEN 0
         ELSE MAX(CAST(ISNULL(Price, 0) AS BIGINT)) - MAX(CAST(ISNULL(Price_Excl_VAT, 0) AS BIGINT))
    END AS VAT
   ,MAX(CAST(ISNULL(Price, 0) AS BIGINT)) AS Price
   ,MAX(CAST(ISNULL(Dep_Amount, 0) AS BIGINT)) AS Dep_Amount
FROM uvw_HotelAllotmentToBook
WHERE Client_Id = 'PCWHK'
    AND Project_Id = 'INTA14'
    AND Allotment_Date >= '2014-05-11'
    AND Allotment_Date < '2014-05-14'
GROUP BY Hotel_Id
   ,Hotel_Name
   ,Room_Category
   ,Room_Category_Name
   ,Room_Type
   ,Room_Type_Name
   ,Info_URL_Title
   ,Info_URL
   --,Dep_Amount
HAVING COUNT(Allotment_Date) >= 3

I keep getting the same the overflow error but the moment I comment out this line

   ,MAX(CAST(ISNULL(Dep_Amount, 0) AS BIGINT)) AS Dep_Amount

The error is gone.

The problem is that I need the Dep_Amount in the result.

Any help would be very much appreciated.

¿Fue útil?

Solución

Found the problem!

The problem was in the view.

On this line where it was generating the Dep_Amount column

CASE 
WHEN COALESCE(hap.Charge_Dep_Amount, 0) = 0 
THEN COALESCE(hap.Dep_Amount, 0) 
ELSE (COALESCE(CAST(hap.Dep_Amount AS numeric), 0) * COALESCE(CAST(hap.Price AS numeric), 0)) / 10000 
END AS Dep_Amount, 

Now that I'm casting the else as numeric the error is gone!!! :)

Otros consejos

You do not need to cast columns that are just being returned without any operations performed on them. I would run query in the following way and just uncomment one line at the time to figure out where the problem is. It is likely that very last line HAVING COUNT( is what is breaking

SELECT Hotel_IdAS Hotel_Id
   ,Hotel_Name
   ,Room_Category AS Room_Category
   ,Room_Category_Name
   ,Room_Type AS Room_Type
   ,Room_Type_Name
   ,Info_URL_Title
   ,Info_URL
   ,COUNT(Allotment_Date)
   --,MAX(CAST(ISNULL(Price_Excl_VAT, 0) AS BIGINT)) AS Price_Excl_VAT
   --,CASE WHEN MAX(CAST(ISNULL(Price_Excl_VAT, 0) AS BIGINT)) = 0 THEN 0
   --      ELSE MAX(CAST(ISNULL(Price, 0) AS BIGINT)) - MAX(CAST(ISNULL(Price_Excl_VAT, 0) AS BIGINT))
   -- END AS VAT
   --,MAX(CAST(ISNULL(Price, 0) AS BIGINT)) AS Price
   --,MAX(CAST(ISNULL(Dep_Amount, 0) AS [bigint])) AS Dep_Amount
FROM uvw_HotelAllotmentToBook
WHERE Client_Id = 'CLIENT'
    AND Project_Id = 'PROJECT'
    AND Allotment_Date >= '2014-05-11'
    AND Allotment_Date < '2014-05-14'
GROUP BY Hotel_Id
   ,Hotel_Name
   ,Room_Category
   ,Room_Category_Name
   ,Room_Type
   ,Room_Type_Name
   ,Info_URL_Title
   ,Info_URL
   ,Dep_Amount
--HAVING COUNT(Allotment_Date) >= 3
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top