SQL - Rounding decimals from a count sum without a column - mysql

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))

Related

SQL SELECT everthing with value 5 but not specify from which column

I have tried to select something with SQL, and I've a problem with it.
What I want:
SQL SELECT * FROM table WHERE ? = '5';
Select everything which = 5, BUT not specify from which column.
Example:
From this ""database"", you should receive the 1st and the last row.
Is that possible?
You have to list the columns but you can use in. The where clause looks like:
where 5 in (price, height)
Note: This assumes that the columns have the same type. You could get type conversion errors if they are not.
Also, given the names of the column and the data, I assume that the columns are stored as numbers. Hence, I dropped the single quotes around 5. If they are really strings, then use the single quotes.
you need to add a condition to your query with or keyword so if any of them match the row will be shown as a result
SELECT * FROM tablename WHERE price =5 or height= 5
better you list your columns by name instead of using * after SELECT

mysql using select with an arithmetic expression

I am trying to use select in combination with an arithmetic expression
SELECT * FROM `mytable` ORDER BY (`column1` / max(`column1`)* `column2`);
The problem is that it return a single row rather than all the rows sorted base on the expression.
Any idea?
Just order by column1. Dividing by a constant doesn't change the order.
EDIT: Add your expression as another field in the select. E.g.,
SELECT *, x+y as z ORDER BY z
See https://stackoverflow.com/a/10762333/2877364 for a full example of a similar situation.

How to sort the columns in the mysql database

I am having column named rating in the mysql database table with multiple values from 1+,2+,................9+,10+,12+. when i am sorting this column with query
select * from tbl_app order by rating desc
I am getting 9+ as highest value, can any one tell me how to get 12+ as highest value
SELECT rating,SUBSTR(rating,1,LENGTH(rating)-1) FROM tbl_app ORDER BY CAST(SUBSTR(rating,1,LENGTH(rating)-1) as SIGNED) DESC;
if the last char is always a '+',the sql above will work.
what have you kept the datatype of the column rating ? If you have kept it varchar or text then this query will not work for sorting values as per descending order.
Probably the easiest thing to do in MySQL is cast those odd looking strings to numbers:
order by cast(rating as unsigned) desc
-- or less explicitly
order by rating + 0 desc
Both of those casts will stop trying to convert the string to a number when they hit the + so you'll get them sorted numerically.
Simply removing the plus signs from the strings will still leave you with strings and '10' < '2' is just as true for strings as '10+' < '2+'. That's actually your whole problem: you're storing numbers as decorated strings when you should be storing them as integers and adding the + decorations when you display them. You really should fix your schema to make sense instead of adding ugly hacks to work around your schema's strange ideas.
try this :
select convert(replace(rating,'+',' '),unsigned integer) as x from tab order by x desc
sql_fiddle_demo

Constraining returned value decimal places in mySQL

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

mysql sort string number

I have a column of type varchar that stores many different numbers. Say for example there are 3 rows: 17.95, 199.95 and 139.95.How can i sort that field as numbers in mysql
Quickest, simplest? use * 1
select *
from tbl
order by number_as_char * 1
The other reasons for using * 1 are that it can
survive some horrendous mishaps with underflow (reduced decimal precision when choosing what to cast to)
works (and ignores) columns of purely non-numeric data
strips numeric portions of alphanumeric data, such as 123A, 124A, 125A
If you need to sort a char column containing text AND numbers then you can do this.
tbl contains: 2,10,a,c,d,b,4,3
select * from tbl order by number_as_char * 1 asc, number_as_char asc
expected output: 2,3,4,10,a,b,c,d
If you don't add the second order by argument only numbers will be sorted - text actually gets ignored.
Use a CAST or a CONVERT function.
This approach is helpful when sorting text as numbers:
SELECT `my_field`
FROM `my_table`
ORDER BY `my_field` + 0;
Found the solution on http://crodrigues.com/trick-mysql-order-string-as-number/.
Pad the string with leading zeroes:
ORDER BY LPAD(`column`,<max length of string>,"0")
If you really have to you can do this if your source data is compatible:
SELECT column FROM table ORDER BY CAST(column AS DECIMAL(10,2))
It's not going to be very fast for large data sets though. If you can you should change the schema to use DECIMAL in the first place though. Then it can be properly indexed for better performance.