I am looking for some help.
I am running the following in mySQL
"SELECT AVG(`readingValue`) AS `readingValue` FROM table
This returns the value - 0.0282982
If possible I would like to return only 4 digits rounded up/down to closest.
In this case 0.0283.
Is this possible in the select string, or will I need to do it in php?
p.s readingValue column is float(4,3)
Would the ROUND function be an option?
SELECT ROUND( AVG(`readingValue`), 4 ) AS `readingValue` FROM table
Related
I am new to the community so please bear with me. I am working on a sum function that will take the values of 3 columns (Exchange, Commission, Otherfees) and give me that total based on row. The datatypes for these 3 fields are VARCHAR. I started by using a CONVERT function and then addressed any NULLs. Please see the query below:
SELECT SUM(
(SELECT(SELECT
CONVERT(decimal(18,4), isnull(ExchangeFee,0)) AS decimal
FROM T_TABLE) as EXCHANGE_VALUE) +
(SELECT(
SELECT
CONVERT(decimal(18,4), isnull(Commission,0)) AS decimal
FROM T_TABLE) AS COMMISSION_VALUE) +
(SELECT(
SELECT
CONVERT(decimal(18,4), isnull(OtherFees,0)) AS decimal
FROM T_TABLE) AS OTHERFEES_VALUE) AS decimal) AS SUMMED_VALUE
When running this query, I get the message
'SUM' is not a recognized built-in function name.
Please let me know your thoughts.
You could start by using the correct data types for your fields.
ExchangeFee, Commission and OtherFees are all numeric, so why store them in a varchar?
If the values should never be NULL, and here these look like they probably probably shouldn't, set them as NOT NULL and default them to 0.
That said, mysql will convert strings to numbers in a numerical context so you only need to worry about any NULL values which COALESCE or IFNULL will deal with.
As for the query which you want to sum the rows, all of the data is coming from T_TABLE so the general structure of the query should be:
SELECT COALESCE(ExchangeFee,0) + COALESCE(Commission,0) + COALESCE(OtherFees,0) AS SUMMED_VALUE
FROM T_TABLE;
I have above table and I want to get the highest value from table bids where bid_id=60 using the following query
SELECT MAX(offer_amount) as maz FROM bids WHERE bid_id = 60
The problem is I'm getting the result as 80 in instead of the correct value which is 7000000
Anybody with an idea of how to solve this?
Store offer_amount in a numeric field (such as integer or decimal), not as text. Quick solution is to use the CAST() function in the query to cast the field's data type to a numeric one.
I have a database table called druginfo. It contains prices in WSprice column. Type of WSprice column is Double. This means It can contain like 23.5698 values. But I want to show all the values in the column WSprice 2 decimal places rounded like 23.57. How to apply that to all values in the column? Help me to do this.
If you only want to display your DOUBLE column to 2 decimal places, you can use the ROUND function:
SELECT ROUND(column_name, 2)
FROM your_table
This will display a value of 23.5698 as 23.57 in the result set.
If you want to change the format of the entire column you can use this:
ALTER TABLE your_table MODIFY column_name DECIMAL(9, 2)
I think we need to use CAST() instead of ROUND().
The reason behind is ROUND() return decimal values when decimal value exists in the database.
Example:
SELECT ROUND(columnName, 2)
FROM tableName
if columnName = 10.5 Output will be like 10.50
But if columnName = 10 Output will be like 10
And CAST() will return decimal value. But we need DECIMAL with CAST
SELECT CAST(columnName AS DECIMAL(10,2))
FROM tableName
The output will be like 10.00
How can I round the decimals from a count sum without having a column. A column is required in the use of ROUND(), so I am clueless. I'm trying not to create any more columns.
Here is what I have done so far and which works, but it displays too many decimals (4 after the zero). Please note that the reason that the SELECT phrase is in brackets is because it's in another SELECT phrase). What matters is that my code works, but I can't get rid of the decimals...
(SELECT (COUNT(v.id) * r.res_cpm/1000)
FROM databasename_viewcounter v
WHERE v.subject_id = r.subject_id) AS cpm_revenue
FROM databasename_resources r
WHERE r.order_id=:order_id
ORDER BY r.beginning ASC");
The following function CASTS the select statement as decimal
cast((COUNT(v.id) * r.res_cpm/1000)as decimal (10,2))
I am trying to query a table in mysql based on the length of a string in a specific column. I know mysql has a function called LENGTH(), but that returns the length of the string. I want to be able to pull data based on the result of the LENGTH() function.
Example:
SELECT * table WHERE LENGTH(word) = 6
of course that does not work. I read through http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function%5Flength but could not find anything to help me.
yes I could make something in PhP to accomplish this, but I would like to do it at the query level.
Any help?
Try:
SELECT *
FROM table
WHERE LENGTH(RTRIM(word)) = 6
I believe you wanted to use query SELECT * FROM tableName WHERE LENGTH(word) = 6; (assuming that the word is name of column in tableName).
This is very unfortunate solution on large tables, you should create new column and use UPDATE tableName SET wordLength = LENGTH( word).