I am trying to calculate an Avg() for an column with where condition using SQL Server Management Studio 2008. When I try the AVG(column-name) in a SQL query it gives me a rounded value and not a decimal value.
When I copy the dataset into MS Excel I get the accurate value with 4 decimal (example: 10.74) in SQL I am getting just 10. Any help is appreciated.
My query:
SELECT Item, AVG(POOrdrQty)
FROM [tableWR]
where Item ='737' AND POOrdrQty <((select AVG([POOrdrQty]) FROM [tableWR]) * 2)
group by Item
The resulting type of the avg aggregate is the same type as the value you use as parameter. If the field is an int, the result will be rounded to fit the type.
Cast the value that you use as parameter: avg(cast(column-name as float)).
avg(column_name * 1.0) to convert data type to float
Related
I am trying to convert decimal values into non decimal number for a computed column in Oracle SQL.
Please see the below code. But I am not able to get the desired output.
Query:
Select cast(cost_account)*100/cast(amnt_fin) as "computed_LTV"
From Loan_app.
Here I Want a new column name as computed_LTV with the required calculation with no decimal in the output.
It is ROUND you're looking for, I presume.
SELECT ROUND (cost_account * 100 / amnt_fin) AS "computed_LTV" FROM Loan_app
If by "non-decimal number" you mean "integer", just use trunc():
Select trunc(cost_account * 100 / amnt_fin) as computed_LTV
From Loan_app;
I'm analyzing the data of New York City taxi trips of yellow cars in 2018. (You need a Google BigQuery account to access this data set.)
The schema says that most of the columns are numeric. However, when I tried to calculate the sum of the key dollar figures (tip_amount, tolls_amount, total_amount), I got an error message saying that they are string variables.
SELECT sum(total_amount)
FROM [bigquery-public-data:new_york_taxi_trips.tlc_yellow_trips_2018]
WHERE month(dropoff_datetime) = 12
Error: Field total_amount is of type STRING which is not supported for SUM
I then tried to use the cast() function to convert it to a numeric variable, but that did not work.
SELECT sum(total_amount_numeric) FROM
(
SELECT cast(total_amount as numeric) as total_amount_numeric
FROM [bigquery-public-data:new_york_taxi_trips.tlc_yellow_trips_2018]
WHERE month(dropoff_datetime) = 12
)
Error: Field total_amount_numeric is of type STRING which is not supported for SUM
How can I analyze these numeric variables as I intended, instead of the string variables as they are erroneously set in the database?
Below is for BigQuery Standard SQL
#standardSQL
SELECT SUM(total_amount)
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2018`
WHERE EXTRACT(MONTH FROM dropoff_datetime) = 12
The problem you had is because NUMERIC data type is not supported by BigQuery Legacy SQL and rather is treated as STRING and cannot CAST to neither FLOAT nor INTEGER
So, the workaround is to use BigQuery Standard SQL as in above example - and as you see here you don't need to do any CAST'ing as this field is already NUMERIC
Your query will run as follows in Standard SQL:
SELECT sum(total_amount_numeric)
FROM (SELECT cast(total_amount as numeric) as total_amount_numeric
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2018`
WHERE EXTRACT(month FROM dropoff_datetime) = 12
) x;
You can include this hint before the query to ensure that it is run using standard SQL:
#standardSQL
I'm learning SQL on codecademy
There's an example of nested aggregate function:
SELECT ROUND(AVG(price), 2)
FROM fake_apps;
ROUND() requires a number as first parameter, how can AVG(price) be plugged in here? What's the datatype of it?
If I change the code to:
SELECT ROUND(SELECT AVG(price)
FROM fake_apps, 2)
FROM fake_apps;
The code throws a syntax error.
The below code gives a syntax error because ROUND is a scalar function. It expects exactly one value as the first parameter.
SELECT ROUND(SELECT AVG(price)
FROM fake_apps, 2)
FROM fake_apps;
SELECT AVG(price) FROM fake_apps returns a set of rows.
On the other hand, AVG is an aggregate function. It operates on a set of rows. The return type is numeric.
I don't think it needs nested select query, at least as per your requirement example you have provided. This may be resolved with following query also.
SELECT ROUND( AVG (price), 2 ) FROM fake_apps
DataType should be decimal or numeric with precision (for e.g. numeric (18,3) )
Let me know if I interpreted as wrong question and correct me, so I can try another way.
I have 2 columns which I need to divide sum(cola)/sum(ColB), but I am not getting the desired results since SQL server seems to truncate values after decimal
For eg. I have-
select 281370/1035
is giving 271 using simple division, whereas actual result of division is 271.8550724637681 and I want to display 271.8
I tried
SELECT cast(round(281370/1035,1) as numeric(36,1))
but that results 271.0
In SQL Server, you have to cast the integers to decimal and you could use Round to get desired precision.
SELECT cast(Round(CAST(281370 AS decimal) / CAST(1035 AS decimal),1,1) as decimal(10,1))
The problem is that you given the int number and want a decimal result
try this
select convert(decimal(30,10),281370.0/1035.0)
or
select Round(convert(decimal(30,10),281370.0/1035.0),1,1)
#Stormcloak gives the answer to specifically wanting a single position as a mantissa, however to return an exact answer you could "simply" implicitly change the datatype.
select 281370.0/1035
Returns:
271.855072
In Presto DB:
select (CAST(11 as decimal(8,6))/CAST(7 as decimal(8,6))) as result
result:1.571429
decimal(xp,xs)
xp--> total number of digits(before decimal point+ after decimal
point)
xs--> number of digits after the decimal point
reference: https://prestodb.io/docs/current/functions/decimal.html
im sure i am not the first one to ask this but i can't find the answer to this:
I haver a select query on a datatable in a sqlite database.
select *, ((int_EndTime)-(int_StartTime))/60 as dou_usage_min FROM tbl_unautho_usage;
when i run this i get all the fields from the datatable including a new column calculated from to integer columns with unix time stamp values. However, i want my calculated column to be of the type double. With the query above i get a type integer.
select *, ((int_EndTime as float)-(int_StartTime as float))/60 as dou_usage_min FROM tbl_unautho_usage;
Afterwards I tried to change the column type of my integer-columns to float, but this gies me the following error:
near "as": syntax error:
i got the idea for that from the following post:
How to cast computed column with correct decimal/$ result
Try multiplying a value used within the arithmetic operation by 1.0.
select
*,
((int_EndTime*1.0)-(int_StartTime*1.0))/60 as dou_usage_min
FROM tbl_unautho_usage;
Probably only one value multiplied will be sufficient.
The correct syntax of a CAST expression is "CAST(something AS type)".
But in this case, for the division to be done with floating-point numbers, it is sufficient for at least one of the operands to be a floating-point number:
SELECT *, (int_EndTime - int_StartTime) / 60.0 ...