I am trying to do a select count(*) from table from a table and I am not able to do it because of this error. I am not aware of the number of rows in the table.
I am not doing any other aggregation in my query apart from this.
I guess it has something to do with the count value which is too large to be stored in INTEGER.
What is the alternative?
When your session runs in Teradata mode the result of a COUNT is INTEGER as you already noticed (in ANSI mode it will be a DECIMAL with at least 15 digits).
The workaround is simple, cast it to a bigint:
SELECT CAST(COUNT(*) AS BIGINT)...
This is the #2 Google hit for Teradata 2616, so I want to add something. If you're getting 2616 "Numeric overflow occured" from a SUM in Teradata, the solution is to CAST, then SUM. The CAST has to be inside the SUM:
SELECT
SUM(CAST(WHATEVER_QTY AS DECIMAL(38,0)))
FROM
TER_DATABASE.WHATEVER_TABLE ;
In my case, DECIMAL(38,0) worked, but BIGINT was 2616. You are welcome to experiment. Here's the link at info.teradata:
http://www.info.teradata.com/HTMLPubs/DB_TTU_14_00/index.html#page/SQL_Reference/B035_1145_111A/Data_Type_Conversions.098.297.html
Related
The code below calculates the conversion rate of a dataset. The code relevant to this question is in line 13. When I calculate the conversion rate, I divide the total number of purchases made on the website by the total number of users (people who browse) on the website. The output I get is 0.495 but I don't understand why I need the '1.0 *' at the start of line 13 for this to work? I don't know the purpose of this part of the code, but without it the code doesn't work.
Your code is not MySQL code. MySQL does not support square braces around identifiers. One database that does is SQL Server. And it does integer division. So, in SQL Server, 1/2 is 0 rather than 0.5.
The * 1.0 simply converts the integer to something with a decimal point.
Assuming userid is not NULL, this is more easily expressed as:
avg( is_purchase * 1.0 )
forpas and Gordon Linoff are correct that SQL Server performs integer division by default. In future, and for more complex calculations, you can use CTEs or subqueries employing CAST to represent values as floating point values prior to division. E.g., at lines 6-7 you would have:
CAST(p.userid IS NOT NULL AS float) AS is_purchase
Hi I am trying to find total revenue earned by cabbies from this data set on google big query here but sum(total_amount) doesn't seem to work. It says
Error: Field total_amount is of type STRING which is not supported for
SUM
even though it is defined as numeric data type by default. I did some casting things but then the sum says 0.
I don't seem to find why.
Things I tried:
Specifying the column as INTEGER in SUM like SUM(INTEGER(total_amount)) returned 0.
CASTED the column name to INTEGER using CAST type returned 0.
Checked if there are any NULL columns but there is none.
Here is my query:
Select sum(total_amount) from
[bigquery-public-data:new_york_taxi_trips.tlc_yellow_trips_2018]
What am I missing here?
NUMERIC data type in Legacy SQL has limited support
So, instead try running your query in BigQuery Standard SQL as in below example
#standardSQL
SELECT SUM(total_amount)
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2018`
with result
Row f0_
1 1837861124.95
I've got a simple calculation (910 / 28 = 3.5) and I'm trying to perform this in a SQL query:
SELECT CONVERT(DECIMAL(5,2), (910 / 28),2) AS average
But the answer is coming out at 32.00, I'm obviously missing something simple could someone spare a moment to point out my error please?
Thanks,
C
Use this:
SELECT CONVERT(DECIMAL(5,2), (910.0 / 28)) AS average
By taking the quotient as 910.0 / 28 SQL Server will retain decimal precision. Then, make your cast to a decimal with two places. By the way, as far as I know CONVERT typically takes just two parameters when converting a number to decimal.
we can use this query for dynamic value from table:
SELECT CONVERT(DECIMAL(5,2), (cast (910 as decimal))/ 28) AS average
It will give the desire output
Unsure if this applies to your database, but in Trino SQL (a sort of database middleware layer), I find adding a decimal point followed by two zeros to any of two operands in this query (e.g., select 910.00/23 AS average or select 910/23.00 AS average) returns a non-integer value (39.57, instead of 39).
Adding 3 zeros after the decimal (select 910.000/23 AS average) returns a 3-decimal place result (39.565), and so on.
Try this query...
SELECT CAST(910 AS decimal(5,2)) / CAST(28 AS decimal(5,2)) as average
Try use this
select cast(round(910/28.0,2) as numeric(36,2)) AS average
I have a table in SQL Server where I have the scores for some competencies, I have one score for the standard and one for the actual score. For instance S25 is the actual score and C25 is the standard for the score. I need to find the difference between the two so I can see who was above and below the standard and cannot figure out how to get the subtract to work. THe way I tried was
Select (S25) - (C25) AS 25_Score
Which did not work
If table starts with a number, bracket it, and that might work. What error do you get?
select (S25)-(C25) AS [25_Score]
from table_name
Your query should work if your columns are a numeric datatype.
The only issue I see is you are starting the alias with a number. You will need to escape the number value with a square bracket:
Select (S25) - (C25) AS [25_Score]
from yt;
See Demo
It may be that the column is of varchar so you have to convert
select convert(int,[S25])-convert(int,[C25]) AS [25_Score]
from table_name
I have both positive and negative numbers (money) in a column and need to:
SUM the total ie. SUM(myColumn) based on if the numbers are +/-
Present the result as an absolute ie. even though the result is -1234 it should be presented as 1234
SQL is not my trade as you probably notice but we've solved most other issues but this one so any help is appriciated. Keep in mind my skill level is very low
You will have to use a combination of the sum and abs aggregate functions in SQL. Since you want the absolute value of the sum, the sum function will need to be called inside the call to abs:
select abs(sum(columnName)) from table
That should work for both SQL Server, MySQL, and Oracle.
Try one (or more) of these
SELECT SUM(moneyColumn) FROM MyTable
SELECT SUM(ABS(moneyColumn) FROM MyTable
SELECT ABS(SUM(moneyColumn) FROM MyTable