Exclude '-1.79769313486232E+308' from table using select query in SQL - sql

I want to get the average of the data in one of the table but I get "Arithmetic overflow error converting expression to data type float." exception. My table looks like,
I have to get the average of floating points in the table.
I want to exclude '-1.79769313486232E+308' from the table. How to do that?
Query that I have,
SELECT PS.Name, Z.[Round]
,AVG(Z.[EstimatedValue])
,AVG(Z.[InformationGain])
FROM [dbo].[IntermediateScores] Z
INNER JOIN [dbo].[PScale] PS
ON Z.[ScaleId] = PS.Id
GROUP BY PS.Name, Z.[Round]
ORDER BY PS.Name,Z.[Round]

Assuming that you are running SQL Server (which the syntax of your query and the error message that you showed tend to indicate), you could use TRY_CAST() to handle values that do not fit in the FLOAT datatype. When the conversion fails, TRY_CAST() returns NULL, which aggregate function AVG() ignores.
SELECT
PS.Name,
Z.[Round],
AVG(TRY_CAST(Z.[EstimatedValue] AS FLOAT)),
AVG(TRY_CAST(Z.[InformationGain] AS FLOAT))
FROM [dbo].[IntermediateScores] Z
INNER JOIN [dbo].[PScale] PS ON Z.[ScaleId] = PS.Id
GROUP BY PS.Name, Z.[Round]
ORDER BY PS.Name,Z.[Round]
In general, you want to avoid relying on implicit conversion; it seems like your data is not stored in a numeric datatype, which is the root cause of your problem. Explicit conversion is a better practice, since it is easier to debug when things go wrong.

Related

How to SELECT a postgresql tsrange column as json?

I want to get a tsrange column returned as json but do not understand how get it to work in my query:
reserved is of type TSRANGE.
c.id and a.reservationid are of type TEXT.
-- query one
SELECT DISTINCT c.*, to_json(a.reserved) FROM complete_reservations c
JOIN availability a ON (c.id = a.reservationid)
throws
ERROR: could not identify an equality operator for type json
LINE 1: SELECT DISTINCT c.*, to_json(a.reserved) FROM complete_reser...
^
SQL state: 42883
Character: 22
it works if i try it like
-- query two
SELECT to_json('[2011-01-01,2011-03-01)'::tsrange);
Result:
"[\"2011-01-01 00:00:00\",\"2011-03-01 00:00:00\")"
and I do not understand the difference between both scenarios.
How do I get query one to behave like query two?
as pointed out in this comment by Edouard, there seems to be no JSON representation of a tsrange. Therefore I framed my question wrong.
In my concrete case it was sufficent to turn the tsrange to an array of the upper() and lower() values of the TSRANGE and cast those values as strings. this way i can use the output as is and let my downstream tools handle them as json.
SELECT DISTINCT c.*, ARRAY[to_char(lower(a.reserved),'YYYY-MM-DD HH:MI:SS'),to_char(upper(a.reserved),'YYYY-MM-DD HH:MI:SS')] reserved FROM complete_reservations_with_itemidArray c
JOIN availability a ON (c.id = a.reservationid)
which returns a value like this in the reserved column: {"2023-02-04 04:57:00","2023-02-05 04:57:00"} which can be parsed as json if needed.
I post this for reference. I am not sure if it exactly answers my question as it was framed.

Error with group by in DB2 - SQLSTATE 42803

I've got a SQL script and its failing. This is what it looks like:
SELECT P.SOORT,P.TYPEBETALING, P.MIDDELCODE, P.HOEVEELHEID, P.EENHEID, P.BEDRAG,P.MIDDEL
FROM DAAO01.BETALINGEN as A join
DAAO01.KLANTEN P
on p.KLANT_ID = 1
GROUP BY P.TYPEBETALING
ORDER BY P.TYPEBETALING
When I execute this I get an error:
COLUMN OR EXPRESSION IN THE SELECT LIST IS NOT VALID. SQLCODE=-122, SQLSTATE=42803, DRIVER=4.18.60
What am I doing wrong?
It is quite difficult to tell what you're trying to do without seeing your data.
But the error is saying that you have not specified how you are going to deal with the rest of the fields in your Group by aggregation:
P.SOORT, P.MIDDELCODE, P.HOEVEELHEID, P.EENHEID, P.BEDRAG, P.MIDDEL
If they're numbers, then you could sum them or take the avg etc.
If they're strings, then you either need to group by them or remove them from your selection.

Conversion failed when converting the varchar value '6276.54' to data type int

I have the below query that keeps throwing the conversion error. I have tried so many ways of making this work but still get the same error. Process of elimination I'm now thinking that this is to do with the hard coded 0.715 value. I'm not sure if or how I force this to be a decimal or if its better to have this SET and a variable at the beginning of my code.
SELECT CAST(DATEPART(yyyy, YEAR) as INT) AS YEAR,
COUNT(ID) AS ID,
SUM(VALUE) AS VALUE,
SUM(PREMIUM) AS PREMIUM,
SUM(try_convert(float,USERID1)) AS VALUE1,
COALESCE(SUM(PREMIUM)/(SUM(try_convert(float,NULLIF(USERID1,0)))*0.715),0) AS 'PROFIT'
SUM(CAST(USERID1 AS float))/SUM(PREMIUM) AS 'PROFIT%'
INTO #YEAR_VALUE
FROM #DATA LT
INNER JOIN dbo.AccountDB P ON LT.ID = P.ID
GROUP BY INCEPTDATE
Any help would be much appreciate.
Thanks in advance
You have some logic back to front, you need to try_parse first, and then do nullif. Try_parse generally returns null if it cannot cast to type specified. You then need to convert null to 0.0
coalesce(sum(premium)/(sum(nullif(try_convert(float,'userId), 0.0)) * 0.715), 0.0) AS 'profit'
Also, use 0.0 for zero if expecting floats or decimals
Hint: save yours and the next person eyes, no need to use uppercase. That was back in the 70's
As it has been mentioned, It could be possible that you have some non-numerical data within that column. I did the best I could with what I can gather from here to throw together a quick mock up and it seems to work for me with data I have entered. Just testing the line of code you seem to be having issues with.
USE DB
GO
CREATE TABLE
Test_Table (
Premium decimal,
userID varchar(20)
)
INSERT INTO
Test_Table VALUES
(
2.5, 1
)
USE DB
GO
SELECT COALESCE(SUM(Premium)/(SUM(try_convert(float,NULLIF(userID,0)))*0.715),0) AS 'PROFIT'
FROM Test_Table;
That may point to either some non-numerical data or another line of code being the issue. Just food for though, I know our data and data types probably aren't the exact same.

Error when aggregating due to conversion from varchar to bigint

Situation:
I want to aggregate a value from a table but i get the following error :
Error converting data type varchar to big int.
I've been reading countless of different solutions online but they don't seem to solve it.
Current query;
So based on the error message, i simply added the CAST function but it still doesnt work.
SELECT
base.target_date AS target_date
, base.game_id AS game_id
, base.device AS device
, ISNULL(CAST(SUM(use_point.point) AS bigint),0) AS result
FROM
cte AS base
LEFT JOIN cte2 AS use_point
ON base.target_date = use_point.target_date
AND base.game_id = use_point.device
AND base.device = use_point.device
GROUP BY
base.target_date
, base.device
, base.game_id
WITH ROLLUP
GO
I'm presuming that use_point.point is a VARCHAR, in which case simply change where you put your CAST statement:
, ISNULL(SUM(CAST(use_point.point AS bigint)), 0) AS result
Note that the CAST now takes place before the SUM.
Use TRY_CAST() instead. And it needs to be an argument to the SUM():
SELECT base.target_date, base.game_id, base.device,
COALESCE(SUM(TRY_CAST(use_point.point as bigint)), 0) as result
FROM . . .
Note that your column aliases are redundant, because you are assigning the default aliases.
You should also fix the data. Don't store numeric values as strings. To find the bad data, you can use:
select points
from use_points
where try_convert(points as bigint) is null and
points is not null;

Why would YEAR fail with a conversion error from a Date?

I got a view named 'FechasFirmaHorometros' defined as
SELECT IdFormulario,
CONVERT(Date, RValues) AS FechaFirma
FROM dbo.Respuestas
WHERE ( IdPreguntas IN (SELECT IdPregunta
FROM dbo.Preguntas
WHERE
( FormIdentifier = dbo.IdFormularioHorometros() )
AND ( Label = 'SLFYHDLR' )) )
And i have a Function named [RespuestaPreguntaHorometrosFecha] defined as
SELECT Respuestas.RValues
FROM Respuestas
JOIN Preguntas
ON Preguntas.Label = #LabelPregunta
JOIN FechasFirmaHorometros
ON FechasFirmaHorometros.IdFormulario = Respuestas.IdFormulario
WHERE Respuestas.IdPreguntas = Preguntas.IdPregunta
AND YEAR(FechasFirmaHorometros.FechaFirma) = #Anio
AND MONTH(FechasFirmaHorometros.FechaFirma) = #Mes
#LabelPregunta VARCHAR(MAX)
#Anio INT
#Mes INT
I keep getting this message upon hitting the aforementioned function while debugging another stored procedure that uses it
Conversion failed when converting date and/or time from character string.
Yet i can freely do things like
SELECT DAY(FechaFirma) FROM FechasFirmaHorometros
Why is this happening and how can i solve or work around it?
I assume that RValues is a string column of some type, for some reason. You should fix that and store date data using a date data type (obviously in a separate column than this mixed bag).
If you can't fix that, then you can prevent what Damien described above by:
CASE WHEN ISDATE(RValues) = 1 THEN CONVERT(Date, RValues) END AS FechaFirma
(Which will make the "date" NULL if SQL Server can't figure out how to convert it to a date.)
You can't prevent this simply by adding a WHERE clause, because SQL Server will often try to attempt the conversion in the SELECT list before performing the filter (all depends on the plan). You also can't force the order of operations by using a subquery, CTE, join order hints, etc. There is an open Connect item about this issue - they are "aware of it" and "hope to address it in a future version."
Short of a CASE expression, which forces SQL Server to evaluate the ISDATE() result before attempting to convert (as long as no aggregates are present in any of the branches), you could:
dump the filtered results into a #temp table, and then subsequently select from that #temp table, and only apply the convert then.
just return the string, and treat it as a date on the client, and pull YEAR/MONTH etc. parts out of it there
just use string manipulation to pull YEAR = LEFT(col,4) etc.
use TRY_CONVERT() since I just noticed you're on SQL Server 2012:
TRY_CONVERT(DATE, RValues) AS FechaFirma