This question already has answers here:
Format number to 2 decimal places
(6 answers)
Closed 8 years ago.
I need to change my query to return only 2 decimal points
SELECT (AVG(cost)) AS 'Average Cost $'
FROM donuts
WHERE cost;
I don't want to use the ROUND function because I wont get a decimal.
SELECT ROUND(AVG(cost)) AS 'Average Cost $'
FROM donuts
WHERE cost;
Any Suggestions,
You will get decimal point with the mysql round() function. You have to pass number of decimal points you wish to display. Default value is 0.
For example, in order to get 2 decimal points you will run this query:
SELECT ROUND(AVG(cost), 2) AS 'Average Cost $'
FROM donuts
WHERE cost > 0;
http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_round
Related
This question already has answers here:
How can I make the decimal places of AVG function in sql limit to 2 only?
(3 answers)
Closed 6 days ago.
SELECT AVG(
TIMESTAMPDIFF(MINUTE,
customer_service_ticket_raise_time,
CURRENT_TIMESTAMP()
)
) AS avg_waiting_time
FROM customer_service_ticket;
I am writing this SQL query to display the average waiting time customers have been waiting but it outputs as a decimal value e.g. 29.8333. Is there any way I want get rid of the decimals and display the minutes as a whole value.
you can wrap your value with the CEILING function
https://www.w3schools.com/sql/func_sqlserver_ceiling.asp
I am trying to find max invoice:
SELECT IFNULL(MAX(SUBSTRING_INDEX(invoice,'I', -1)) + 1, 1) AS invoice
FROM sales
SQL Fiddle
When I run this SQL query, it can not count more than 10.
invoice
20221026P1I1
20221026P1I2
20221026P1I3
20221026P1I4
20221026P1I5
20221026P1I6
20221026P1I7
20221026P1I8
20221026P1I9
20221026P1I10
20221026P1I11
20221026P1I12
I am trying to find max invoice 12 + 1 = 13
Your use of SUBSTRING_INDEX() is correct, however you should cast the string value to a bona fide integer:
SELECT COALESCE(MAX(CAST(SUBSTRING_INDEX(invoice, 'I', -1) AS UNSIGNED)), 1) AS invoice
FROM sales;
The problem with trying to find the max of the text substrings themselves is that text numbers sort lexicographically, e.g.
1
10
11
2
23
But this isn't the behavior you want, you want the numeric maximum. Hence we should cast these substrings and then compare.
Side note: You could have avoided this problem entirely by maintaining a pure numeric invoice number column. You may want to change your table design to include such a column.
I am writing a query that is used by report generating software.
Part of this is querying for the hours needed to complete a project. We record this a 2 decimal float so that we can estimate to the quarter hour.
However, if we are using it in our report and the hour we recorded is something like 8.00, I want to query it and format it so that 8.00 is just 8. However any hours with something past the decimal, like 8.25, should remain as 8.25. How can I make this work?
hours Queried Result
====== -> My Query -> ==============
8.00 8
8.25 8.25
I am using MySQL 5.6
You can use the REPLACE() function to remove .00:
REPLACE(hours, '.00', '') AS hours
You can convert it to a string and check the rightmost 2 characters and trim those if they are '00'.
SELECT TRIM(TRAILING '.00' FROM CAST(column_name AS VARCHAR));
SELECT REPLACE(Round(8.00), '.00', ' ');
I will give more example so you can clear your Logic:
MySQL ROUND() rounds a number specified as an argument up to a number specified as another argument.
Syntax:
ROUND(N,[D]);
Where 'N' is rounded up to D decimal places.
and 'D' is indicating up to how many decimal places N will be rounded.
Example 1:-
SELECT ROUND(4.43);
Output :-
4
The above MySQL statement will round the given number 4.43. No decimal places have been defined, so the default decimal value is 0.
Example 2:-
SELECT ROUND(-4.53);
Output:-
-5
The above MySQL statement will round the given number -4.53. No decimal places have been defined, so the default decimal value is 0.
This question already has answers here:
How to find the maximum count using mysql?
(9 answers)
Closed 4 years ago.
select max(total),blockfloor
from
(select blockfloor,count(roomnumber) as total
from room
where unavailable='0'
group by blockfloor) x;
Above code is mysql query which I used to find the blockfloor, which has maximum number of rooms available.I don't have clear understand about roll of x here.Can anyone explain what is the roll of x?
In your sql query x is alias for the temp table, where will be data from subquery.
for find max total row, you can use query
select blockfloor,count(roomnumber) as total
from room
where unavailable='0'
group by blockfloor
ORDER BY total DESC
LIMIT 1
where you order rows by total, and get first row with max value.
Using MySQL 5.0.27
This query:
SELECT CAST('543.21' AS DECIMAL(100,2))
returns 543.21
So does this one:
SELECT CAST('543.21' AS DECIMAL(2,2))
In fact, I am having trouble figuring out what effect the parameter has. I am using it to aggregate numeric values in a varchar column (for legacy reasons!!) and round off to 2 decimal places.
Should I just pick a high number?
It describes how many total digits a field (or variable) will be able to store.
DECIMAL(100,2) - 100 total digits, 98 before, 2 after a decimal separator
DECIMAL(2,2) 2 total digits, 0 before, 2 after a decimal separator
Explained here:
http://dev.mysql.com/doc/refman/5.1/en/numeric-types.html
[added]
For rounding just use ROUND() function.