Getting SUM of MySQL table data when there is floating point numbers - mysql

I an using MySQL database and I have a table called fertilizer_storage which is using both plus and minus values. It has 4 columns uria,TSP,MOP and TDM
I am using double as data type and getting sum of each column using follwing syntax,
SELECT SUM(uria),SUM(TSP),SUM(MOP),SUM(TDM) FROM `fertilizer_storage` WHERE `branch_ID`=1
The problem is for some columns I get unwanted floating points of 15 while all columns are containing numbers up to 4 floating points.
7.666900000000002
7.666900000000002
9.6109
9.9924
when I changed numbers in first two columns as other two it gives the correct answer. what should I do to correct this.

You can use ROUND(X, D):
Rounds the argument X to D decimal places. The rounding algorithm
depends on the data type of X. D defaults to 0 if not specified. D can
be negative to cause D digits left of the decimal point of the value X
to become zero.
SELECT Round(Sum(uria), 4),
Round(Sum(tsp), 4),
Round(Sum(mop), 4),
Round(Sum(tdm), 4)
FROM `fertilizer_storage`
WHERE `branch_id` = 1
See it in action

Related

How do I amend all values in a column to a fixed 9 digits and the results of each beginning with a constant int 405 eg.51=405000051

I need to export some customer balances from a table and import to another. However the customer_ID column is not fixed and need to be, the values are anywhere from 3 to 5 digits (eg.322…4144…11833).
I would like to export the data with the customer_ID and customer_bals to a csv where the values in the customer_id column are fixed to 9 digits all starting with a constant 405 at the beginning of each value. Such as eg. 405000322…405004144 and 405011833.
How would I write this query in MySQL Workbench?
Use the LPAD function to add leading zeroes, and CONCAT() to concatenate the fixed prefix.
CONCAT('405', LPAD(customerID, 6, '0'))

Get values from database with only 2 decimal places using MySQL

I want to retrieve values from a table with figures formatted in float. The decimal places range up to thirteen. I only want to retrieve the rows whose values only have exactly 2 decimal places. How do I do this?
Expected output:
[45.678, 56.236656457, 89.23, 100.89] ==> [89.23, 100.89]
You can use select Length(123.12 % 1) - 2 to get the length of the values after the decimal.
select (123.12 % 1) returns 0.12 so you always know that you have 0. in your result set, you can then get the length of your value minus the two characters and only select the rows where length = 2.

How can I round up the numbers of mysql table, leaving the two decimals to zero. it's possible?

I need round up the numbers of a complete table of mysql with a funtion leaving the two decimals to zero?
e.g.:
from 75,55 to 86,00
from 75,45 to 75,00
All the prices of a complete table changed a few decimal and I do not know how it happened.
Can anybody help me?
You can simply use Round(X, D) function. From Docs:
Rounds the argument X to D decimal places. The rounding algorithm
depends on the data type of X. D defaults to 0 if not specified. D can
be negative to cause D digits left of the decimal point of the value X
to become zero.
Try:
SELECT ROUND(75.45, 2);
-- Fetches 75.00
SELECT ROUND(75.55, 2);
-- Fetches 76.00
Now, it seems that you want to Update all the values of a particular column, by rounding them to two decimal places. You can do the following:
UPDATE your_table_name
SET your_column_name = ROUND(your_column_name, 2);
-- It will round all the values to 2 decimal places.
"UPDATE your_table_name SET your_column_name = ROUND(your_column_name, 2);" not work for me. I dont know.
¡SOLUTION! = TABLE > ESTRUCTURE > NAME COLUMN > length/values > remove decimal.
All number round up in my row (column). Them I put decimal again. All my fields it's ok.

Check decimal places that are not 0

Currently I have this field in my database that is a (16, 10) double. I need to change this to a ( 16, 4 ) double. the only problem is that I have a lot of records and I need to check if some any of those records are using more than 4 decimal places. Something like 1500.3333333. The problem is that by changing just changing the field to (16,4) I get zeros to the right, like 14,5500.
Is there any way, using Mysql that I can check if the double is using more than 4 decimal places that are not 0?
Use CHAR_LENGTH() and LIKE functions, like this:
SELECT * FROM table
WHERE CHAR_LENGTH(column) > 4
AND column NOT LIKE "%0"

Anomalous mysql behaviour on replace query

im using a v simple database and i have 3 columns A(bigINT 20) , B(bigInt 20) and c(DECIMAL(5,4)) , when i fire the following query i get the below mentioned results :
REPLACE INTO `my_table` SET `A` = 8,`B` = 44,`C` = 14;
i get these values in mysql A =8 , b= 44 and c as 9.9999 ! ?
any ideas as to why is this happening and what can i do to resolved this ?
DECIMAL(5,4) means that the number has at most 5 digits, 4 of them after decimal point. So 14 is simply overflow as it would require DECIMAL(6,4).
It must be cleared that 14 is overflow, because as constant precision point decimal it is internally 14.0000 here (so six digits over five).
So if you try to put 14.0000 (six digits) in DECIMAL(5,4) (five digits max) -> MySQL chooses value closest to the one you request. Therefore 14.0000 gets "rounded" to 9.9999.
To fit 14 in your column you can either extend it do DECIMAL(6,4) (to allow more digits in general) or change to DECIMAL(5,3) (which will allow one more digit before decimal point, but loses some precision of course).