How do i sort my data in sql in asc or desc order?
I am using :
ORDER BY DESC
but my data is not in order. The data for my codes are below :
AC1, AC2 ,AC3 ..., AC30,AC31
The sql sorts the data by the first letter and not the value of the data so my data is sorted like this
AC10, AC11, AC12, AC13 ... AC19
Next in line would be :
AC20, AC21, AC22, AC23
I would like my data to be sorted from the very first number (AC1,AC2,AC3...) and not (AC1,AC10,AC11)
The order by is working correctly. Your expectation is wrong. Strings are sorted alphabetically.
You can easily get what you want. Assuming the column starts with two letters, you can do:
order by char_length(col) desc, col desc
If you want the default sort to work, then just pad the numbers . . . AC01, AC02, and so on.
Related
I have an mysql query like this:
select empcode from employee where empcode like 'LAMO%' order by empcode desc limit 1;
and it's result like this,
But my actual records is,
I need output like this LAMO10
The field empcode is having a data type corresponding to characters.
Here, LAMO1 is less than LAMO2 and so on, but observe that the string is compared character by character. Therefore, LAMO10 is smaller than LAMO2 because while comparing from the left, the first 4 characters LAMO are equal, the 5th character 1 in LAMO10 is smaller than the 5th character 2 in LAMO2. So, the order that you would get (if you removed the limit in your query) is:
LAMO9
LAMO8
LAMO7
LAMO6
LAMO5
LAMO4
LAMO3
LAMO2
LAMO10
LAMO1
This explains why you aren't getting your desired output LAMO10. To generate it, you need to order by only the numbers in your string. In this particular dataset, that number you are looking for appears to be everything onward from the 5th character. The corresponding query segment for ordering would be:
ORDER BY CAST(SUBSTR(empcode,5) AS UNSIGNED) DESC
So, putting it in your query:
SELECT empcode
FROM employee
WHERE empcode like 'LAMO%'
ORDER BY CAST(SUBSTR(empcode,5) AS UNSIGNED) DESC
LIMIT 1;
should get you your desired result.
You may use an order by clause which sorts on the numeric component of the employee code:
SELECT empcode
FROM employee
WHERE empcode LIKE 'LAMO%'
ORDER BY CAST(REPLACE(empcode, 'LAMO', '') AS UNSIGNED) DESC
LIMIT 1;
I've been playing around with this API and cannot get the $order parameter to work when paired with DESC:
https://data.cityofnewyork.us/resource/9w7m-hzhe.json?$limit=100&$order=score%20DESC
Not only is the data not being sorted accordingly, but the column name/json key (score) I'm attempting to sort against is being omitted from the query results!
According to Socrata docs, you can set to ascending order by replacing the 'DESC' with 'ASC'. The same can be accomplished by not specifying it at all - it will default to ASC. And both of these work fine when I test.
But I can't get DESC to work at all. Thanks.
For some entries, the score column contains nulls, which are sorted first because SQL is weird.
Try adding a filter for $where=score IS NOT NULL:
GET https://data.cityofnewyork.us/resource/9w7m-hzhe.json?$limit=100&$order=score%20DESC&$where=score%20IS%20NOT%20NULL
I have a table name test. In that I have to sort the Var-char Field. But When I tryed to sort, I got wrong output.
My Table-
My Query -
select * from testtable order by test DESC;
My Output -
What I want,.
test
G450
G145
G56
G45
G4
G3
PLEASE HELP ME TO GET THIS OUTPUT.
If your format is always one letter followed by an integer, a simple way to do this is:
order by length(test) desc, test desc
That is, sort by the length first and then by the field.
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 have a column of states, and, depending on the query, I want to order by results by a particular state, then by id (Asc or Desc, depending). For instance, I might want to show all rows with state "HI", sorted by ID desc, and then all the other rows, sorted by id desc.
I was hoping I could do this in one query, rather than getting all my favored state results, and then getting the rest. Can I?
How about:
SELECT id, state
FROM sometable
ORDER BY IF(state = 'HI', 0, 1) ASC, id DESC;
This will sort 'HI' rows first. If you want them last, change the ASC to DESC.
You have two options:
do a union
write a function and use it to order rows by
In the first case, you could do something like
select 1 as res_order, ...
...
where state like 'hi%'
union
select 2 as res_order, ...
...
where state not like 'hi%'
order by res_order asc, id desc
In the second case, you could do something like
select my_function(state, "hi") as row_order, ...
...
order by row_order
where the function returns lower values for matching states.
The code is off the top of my head: it might need some tweaking to make it runnable.