how to display the value of avg function till only two decimal places in SQL? - mysql

select avg(purch_amt) from orders;
I took an average of the amounts and it shows value till many decimal places and I would like to limit it to two decimal places.
Here's what I have tried:-
select cast(avg(purch_amt) as decimal(10,2)) from orders;
please answer containing both the operations(first average and then conversion). I would like to know the correct syntax of writing both the statements together.

You can update your query to:
select ROUND (avg(purch_amt),2) as Average from orders;

You can use ROUND as
SELECT ROUND(AVG(purch_amt), 2) AS 'Average' from orders
or if you want to Round all column values before calculating average
SELECT AVG(ROUND(purch_amt, 2)) AS 'Average' from orders

My preferred method is to cast to an appropriate decimal value:
select cast(avg(purch_amt) as decimal(10, 2))
from orders;
This is a bit different from round(), although the two generally produce the same result. As the documentation explains for round():
The return value has the same type as the first argument (assuming
that it is integer, double, or decimal)
That means that what gets displayed in an application might still show more (or fewer) than the number of decimal places. By changing the type, the application will know how many decimal places should be in the result.
You can see from this example that round() does not change the data type -- hence it might not change the representation in a tool.

Related

Mysql Query brings up position 0 when datatype is double

I have the following query that positions students according to average mark, but it works well when the data type is an integer, when it is a double or floats it positions some students to be zero, what could be the problem?
SELECT
assessement_progress_reports.student_id
,assessement_progress_reports.student_average
,FIND_IN_SET(assessement_progress_reports.student_average
,(SELECT
GROUP_CONCAT(assessement_progress_reports.student_average
ORDER BY assessement_progress_reports.student_average DESC)
FROM
assessement_progress_reports
WHERE
assessement_id=1
AND student_stream=1)) student_position
FROM
assessement_progress_reports
WHERE
assessement_id=2
AND student_id=123
AND student_stream=2
FIND_IN_SET() is only supposed to be used on strings, not any sort of numbers. it's actually curious that it works on INTs.
you should look into the RANK() function, as I believe it will do exactly what you are trying to do in a more efficient way.

MAX(Column) returns me a wrong value

I want to retrieve the maximum invoice number from my invoice table where it returns the following when I select all records where you can clearly see the maximum invoice no as "10".
select * from invoice
But when I query for
select MAX(invoice_number) as maxinv from invoice
It returns me "9". Why is that?
This situation can occur if your invoice_number is stored as a text column e.g. varchar(10). In that case, based on alphabetical order, 9 will be the maximum value.
Ideally, you should be storing values on which you want to perform numerical operations as numeric datatypes e.g. int. However, if for some reason you cannot change column datatype, you can try casting the column before applying MAX, like so:
select max (convert(invoice_number, signed integer)) as maxinv from invoice
NOTE: I specifically mention "values on which you want to perform numerical operations" because there are cases where the input text is entirely numeric, such as phone numbers or perhaps credit card numbers, but there is no scenario in which you would want to add 2 phone numbers, or take the square root of a credit card number. Such values should be stored as text.
It happens because your column is a VARCHAR, try to cast it to UNSIGNED:
select MAX(CAST(invoice_number AS UNSIGNED)) as maxinv from invoice
As Joshi noticed, if you have negative values you can use SIGNED
select MAX(CAST(invoice_number AS SIGNED)) as maxinv from invoice
It is a pseudo code, try and see it.
SELECT MAX(CAST(invoice_number AS SIGNED)) from invoice.

MySQL - Use min function or limit an order-by query

I am writing a query to find the product with the minimum price.
These are the two queries I tried:
select min(price) from products
and
select price from products order by price limit 1
The first one returns 19.950000762939453 and the second one returns 19.95 which is the accurate value. So my question is, what's the difference of the two queries?, why is the first one weird?! and which has a better performance for this task?
Thanks in advance.
Your data type of price is probably a floating-point with is by definition inaccurate.
If you use a fixed-point data type like decimal it will be 19.95.
You can read it up in the doc
min has better performance, according strange values - you should read how floating numbers are stored in memory/db, they are "rounded"
if you store real price - go with DECIMAL type, it will work fine

How can i do the sumation of time in mysql query?

I have a table like this :
And in this table you can see the last column totalloginFinal, I want to do the sum of all time, its will be about 84 hours,
I am trying with addtime and sum function but not getting proper result.
Thanks a lot in Advance.
Try this variant -
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(totalloginFinal))) FROM table_name;
From the reference - The SUM() and AVG() aggregate functions do not work with temporal values. (They convert the values to numbers, which loses the part after the first non-numeric character.) To work around this problem, you can convert to numeric units, perform the aggregate operation, and convert back to a temporal value.
GROUP BY (Aggregate) Functions.
try convert it to second and then sum the seconds and convert it back from second to time
like :
SELECT SEC_TO_TIME(SUM(SECOND(totalloginFinal))) as Total from mytable

Conditional values for calculated column in MySql

I'm trying to calculate the difference between two date columns, which works just fine as:
select (ship_date - due_date) AS DaysTaken;
Basically, I want to determine if orders were shipped on time and, if not, how many days late. This works fine, as expected, except, if the order is shipped early, I get a negative number. What I'd like to do is to evaluate the results and return 0, if it calculates a negative value.
I don't know how to do this in a MySQL select statement. Can anyone help?
You can use the GREATEST() function to replace negative numbers with 0:
select GREATEST((ship_date - due_date),0) AS DaysTaken;
SELECT GREATEST(ship_date - due_date, 0) AS DaysTaken