One of the column in my table like this:
Symbol
586fast
urgent
243late
296fast
122late
155fast
I need urgent in first then records with fast order asc then records with late order asc like this:
urgent
586fast
296fast
155fast
243late
122late
I am getting urgent in first row,records with fast and late but they are not in asc order
ORDER BY CASE
when Symbol like '%FUT' then 1
when Symbol like '%CE' then 2
when Symbol like '%PE' then 3
else 4 end
After ordering using case statement provide further ordering by Symbol column itself
ORDER BY CASE
when Symbol like '%FUT' then 1
when Symbol like '%CE' then 2
when Symbol like '%PE' then 3
else 4 end, Symbol asc
Related
MB_KOM are first locations returned from this query, then MLL,MDL...etc.
SELECT *, SUBSTRING(location, 7, 20) AS ExtractString FROM inventory
ORDER BY CASE
WHEN location LIKE "MB_BN%" THEN location END ASC,
CASE
WHEN location LIKE "MB_B%" THEN location END ASC,
CASE
WHEN location LIKE "MB_KOM%" THEN ExtractString+0 END DESC
So what am i missing here?
Edit:
Goal is to get locations in this order:
MB_BN% ASC,
MB_B% ASC,
MB_KOM% DESC,
THEN anything else...
After MB_BN and MB_B and MB_KOM are always some numbers.
Example: MB_KOM199, MB_BN010406, MB_B12
You are using ORDER BY with three expressions, so it sorts by the first expression alone, unless that results in a tie. Then it resolves the tie by the second expression. And so on.
Did you mean to use a single CASE expression with multiple cases?
ORDER BY CASE
WHEN location LIKE 'MB_BN%' THEN location
WHEN location LIKE 'MB_B%' THEN location
WHEN location LIKE 'MB_KOM%' THEN ExtractString+0 END
But that has a few problems:
You can't mix ASC and DESC sort order in the same ORDER BY argument.
You can't mix sorting alphabetically and numerically in the same ORDER BY argument. Even though you used the +0 trick to cast the ExtractString to an numeric, it will be cast back to string to be compatible with the expression.
The CASE has no ELSE, so if the location matches none of the patterns, the result of the expression is NULL, and the sorting will be in some arbitrary order.
Here's a different solution:
SELECT *,
CASE WHEN location LIKE 'MB_BN%' THEN 1
WHEN location LIKE 'MB_B%' THEN 2
WHEN location LIKE 'MB_KOM%' THEN 3
ELSE 4 END AS location_type,
REGEXP_SUBSTR(location, '[[:digit:]]+') AS digits
FROM inventory
ORDER BY location_type ASC,
(CASE location_type WHEN 3 THEN -digits ELSE digits END)+0 ASC;
Dbfiddle
How can I make a custom order where I sort the rows by NAME where the first rows' name has Bob in it followed by rows with name of Alex in it?
To explain what exactly I mean: I have made the following query to sort result if NAME = 'Bob' and if NAME = 'Alex':
SELECT * FROM table
ORDER BY CASE `NAME`
WHEN 'Bob' THEN 1
WHEN 'Alex' THEN 2
ELSE 3
END
But this only works when the NAME is exactly equal to Bob or Alex. I want to modify it to sort if the NAME has Bob or Alex in it, essentially if NAME LIKE '%Bob%' and NAME LIKE '%Alex%'. I tried something like the following but it does not work.
ORDER BY CASE `NAME`
WHEN LIKE '%Bob%' THEN 1
WHEN LIKE '%Alex%' THEN 2
ELSE 3
END
What is the correct syntax for this?
Use the other form of CASE where you specify a condition in WHEN rather than a value.
ORDER BY CASE
WHEN NAME LIKE '%Bob%' THEN 1
WHEN NAME LIKE '%Alex%' THEN 2
ELSE 3
END
How can I change this part of the query to have multiple words ordered first in sequence, then the rest of the results alphabetically?
ORDER BY CASE WHEN name LIKE '%Professional%' THEN 0 ELSE 1 END asc, name asc
So, it needs to be:
'%Professional%' 1
'%Leader%' 2
'%Advocate%' 3
'%Clinician%' 4
'%Educator%' 5
'%Scholar%' 6
Then all other results alphabetically.
You can just expand your CASE expression with each of the desired words:
ORDER BY
CASE WHEN name LIKE '%Professional%' THEN 1
WHEN name LIKE '%Leader%' THEN 2
WHEN name LIKE '%Advocate%' THEN 3
WHEN name LIKE '%Clinician%' THEN 4
WHEN name LIKE '%Educator%' THEN 5
WHEN name LIKE '%Scholar%' THEN 6
ELSE 7
END,
name
I am working on an existing project and found this SQL statement. After reviewing the MySQL documentation, I'm a still confused by the syntax and how MySQL is interpreting it.
I have a column called status that can be 0 (inactive), 1 (active), 2 (completed), 3 (testing).
ORDER BY CASE status WHEN 1 THEN 1 ELSE -1 END DESC, id ASC
What confuses me is THEN 1 ELSE -1. In the documentation it mentions that you place a statement_list after THEN. I would expect to see something like this:
ORDER BY CASE status WHEN 1 THEN status ELSE -1 (or some other value that will be ignored) END DESC, id ASC
How is mysql interpreting THEN 1 ELSE -1? I guess I'm curious how this statement is returning the right results. It is basically doing this: If status = 1 then ORDER BY status DESC else ignore the order by statement.
ORDER BY CASE status WHEN 1 THEN 1 ELSE -1 END DESC, id ASC
This means it'll order by (1 or -1 DESC, id ASC) effectively.
Based on the value of status it'll put a 1 or -1 as the first expression to order by and "id ASC" is the tie breaker. Rows with status = 1 will come first in the result (ordered by id in ascending order) and rows with other values for status will be returned after that (their order is not specified any further).
It would order by active status first (1) because of the DESC clause, then everything else would be returned after that. Anything assigned -1 may be randomly ordered.
Hi I want to sort a table .The field contains numbers,alphabets and numbers with alphabets ie,
1
2
1a
11a
a
6a
b
I want to sort this to,
1
1a
2
6a
11a
a
b
My code is, SELECT * FROM t ORDER BY CAST(st AS SIGNED), st
But the result is,
a
b
1
1a
2
6a
11a
I found this code in this url "http://www.mpopp.net/2006/06/sorting-of-numeric-values-mixed-with-alphanumeric-values/"
Anyone please help me
This would do your required sort order, even in the presence of 0 in the table;
SELECT * FROM t
ORDER BY
st REGEXP '^[[:alpha:]].*',
st+0,
st
An SQLfiddle to test with.
As a first sort criteria, it sorts anything that starts with a letter after anything that doesn't. That's what the regexp does.
As a second sort criteria it sorts by the numerical value the string starts with (st+0 adds 0 to the numerical part the string starts with and returns an int)
As a last resort, it sorts by the string itself to get the alphabetical ones in order.
You can use this:
SELECT *
FROM t
ORDER BY
st+0=0, st+0, st
Using st+0 the varchar column will be casted to int. Ordering by st+0=0 will put alphanumeric rows at the bottom (st+0=0 will be 1 if the string starts with an alphanumeric character, oterwise it will be 0)
Please see fiddle here.
The reason that you are getting this output is that all the character like 'a', 'b' etc are converted to '0' and if you use order by ASC it will appear at the top.
SELECT CAST(number AS SIGNED) from tbl
is returning
1
2
1
11
0
6
0
Look at this fiddle:- SQL FIDDLE
I did small change in your query -
SELECT *, CAST(st AS SIGNED) as casted_column
FROM t
ORDER BY casted_column ASC, st ASC
this should work.
in theory your syntax should work but not sure why mysql doesn't accept these methods after from tag.
so created temp field and then sorted that one .
This should work as per my experience, and you can check it.
SQL FIDDLE