Struggling this the following sql statment.
The database is MySQL with score being DECIMAL(5,1)
Im looking to order by the average score within the column
...AVG(COALESCE(score,0)) AS scoreAvg ... ORDER BY scoreAvg DESC
but the results are not as expected, I have products with a high score below ones without a score (score is 0).
It lookings like score is being treated as a string, I have tried.
CAST(SCORE AS DECIMAL(5,1))
but with no luck.
Thank you for any help,
Regards
Try repeating the expression:
ORDER BY AVG(COALESCE(score, 0))
The first explanation that I think of is that scoreavg is already in a table and the ordering is using the column rather than the computed expression.
Your value might be in varchar(max)
So you want to convert the varchar to decimal or numeric and add
Order by avg(COALESCE(column name,0))
Or
Order by avg(coalesce(cast(column name as decimal (5,1)),0.0)
After stripping down my sql string, turns out my "image_path" column in TEXT format was causing the issue. Changed it to varchar(255).
I did'nt post it because I didnt think it was relevent.
anyway fixed now.
Must be a bug in MySQL
Related
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 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
I've got a text field called source_recid. It stores half string half number like strings in it.
Example
shop.orders.32442
the syntax is DATABASENAME.TABLENAME.RECID
My goal is to scan this col and find out the biggest RECID ( the integer) in it.
So, in a case like this
shop.orders.32442
shop.orders.82000
shop.orders.34452
It would be the record whose source_recid is shop.orders.82000. Why? Cause 82000 happens to be the largest integer.
What SQL statement would get me that record?
One option to this is to create a new column ( the_ids ) and move all the integers in it and then run something like this
select source_recid from mytable
where source_recid like 'shop.orders.%'
order by the_ids DESC
LIMIT 1
Is there a way to pull this off without going thru this step?
First of all, unless all of your RECIDs are exactly five characters long forever and always, the select you put up won't work, becuase "shop.order.9" would come out as larger than "shop.order.10", which is wrong.
What I think we need to do here is extract the numeric part, cast it to an integer, and sort by that. Now, I don't have access to mySQL, so this may not be exactly right, but it should be close ...
SELECT
CAST(SUBSTRING_INDEX(RECID,'.',-1) AS INT) AS RecIdNumber
FROM
table
WHERE
RECID LIKE 'shop.order.%'
ORDER BY
RecIdNumber DESC
LIMIT
0, 1
This will take the part after the last dot, convert it to an INT, name it 'RecIdNumber', and sort by that.
I hope this helps.
SELECT CAST(SUBSTRING_INDEX(field,'.',-1) AS INT) AS RID
FROM yourtable
WHERE
RECID LIKE 'shop.order.%'
ORDER BY
RID DESC
I want to find the max value in a column.
Column values are,
E00004,
A00005,
B00011,
H-00001,
E2100112,
EFQ20098,
ESSF20003
I just want to sort the values by their number, Dont mind about the alphabets. It have to be like this, I'm using MYSQL
E2100112,
ESSF20003,
EFQ20098,
B00011,
A00005,
E00004,
H-00001
Assuming the last 5 digits are the number:
select columnName from tableName
order by convert(int, right(columnName, 5)) desc
As #IkeWalker stated, the number can have an arbitrary size.
For it, you'll have to use a while cycle to check the number.
Or, you can have a function do that for you!
Check this article!
I'm getting weird results on a MySQL SELECT statement that uses ORDER BY my_column ASC.
It's ordering the results the way a "computer" would order them, instead of a human:
Item F: 241.565853
Item B: 25.310854
Item D: 25.397155
Item C: 260.252356
Item A: 27.7740
Item E: 271.774058
How do I get it to ORDER BY in the correct manner? My SELECT statement has a couple LEFT JOINS-- not sure if that would make a difference.
Any suggestions on how to correct this problem?
Something like this should do it:
ORDER BY ABS(my_column) ASC
The column you're ordering by is most likely a string-based (varchar, text, etc) datatype. You're seeing lexically-correct ordering for such a datatype. Change the column to use a numeric datatype, or (less-preferably, because why are you storing a numeric value as a string) cast the column to a numeric type and perform the sort on that cast.