How to order by multiple columns in sql - sql

I need help ordering my results, probably with a combination of case statements. Please see image showing existing output, conditions, desired output. Exhibited in Excel for ease, but actually done in SQL. The ORDER BY clause is what I need help on.
select
distinct
CONCAT(selection.Selid,' - ' ,Selection.Name,' - ', DevOfficer.Description,' - ', SchemeCount.[Number of Schemes],' Schemes - ',case when selection.Project=1 then '[Project] ' when selection.Project=0 then '[Selection] ' else 'Error' end,convert(varchar,selection.lastupdated,103)) [String]
, selection.selid
, selection.lastupdated
,case when selection.Project=1 then '[Project]'
when selection.Project=0 then '[Selection]'
else 'Error' end
,selection.lastupdated
from selection
inner join SelScheme on selection.SelID =selscheme.SelID
inner join DevOfficer on selection.DevOfficer = DevOfficer.DevOfficerID
inner join (select selscheme.selid ,count(selscheme.SchemeID) [Number of Schemes] from SelScheme group by SelScheme.SelID) SchemeCount on schemecount.SelID = Selection.SelID
where selection.masterselid = 0
order by
--selection.lastupdated desc,
case
when selection.Project=1 then '[Project]'
when selection.Project=0 then '[Selection]'
else 'Error' End Desc
,selid desc
,selection.lastupdated

this is the order by you need:
order by type desc,
id desc,
case when isdate(last_updated_date) then last_updated_date else 0 end desc
use asc and desc modifiers, for each order-by column you need

* Answered before code snippet added to OP question*
Although I'm unsure of SQL table name and even the actual SQL field names, the below will work.
You need to list all fields in the ORDER BY based on their priority;
SELECT
[ID]
,[Type]
,[Last Updated Date]
FROM [SCHEMA].[TABLENAME]
ORDER BY [Type] ASC, [ID] DESC, [Last Updated Date] DESC

Related

SQL Query problem (2 different order by) in 1 table

This is the original table:
I have 2 different query and I want to make 1 query for these:
SELECT *
FROM SAMPLE
WHERE ORDER_PRIORITY<40
ORDER BY FS_GENERATE_DATE IS NOT NULL, FS_GENERATE_DATE,ORDER_PRIORITY,CREATE_ID,CR_DATE,ORDER_QTY;
second:
SELECT *
FROM SAMPLE
WHERE ORDER_PRIORITY>=40
ORDER BY FS_GENERATE_DATE IS NOT NULL, FS_GENERATE_DATE,ORDER_PRIORITY,CREATE_ID,CR_DATE,ORDER_QTY;
I need the next result in only 1 query:
if the order_priority<40 than the order will be the first according to the order by
but if order_priority>=40 than these data will be after the lower priority (first conditional /op<40/).
Result:
You can add this to your order by clause:
case when ORDER_PRIORITY<40 then 0 else 1 end
The final query will be:
SELECT
*
FROM SAMPLE
WHERE ORDER_PRIORITY>=40
ORDER BY
case when ORDER_PRIORITY<40 then 0 else 1 end,
FS_GENERATE_DATE IS NOT NULL, FS_GENERATE_DATE,ORDER_PRIORITY,CREATE_ID,CR_DATE,ORDER_QTY;
You are clearly using a database where booleans are allowed in the ORDER BY. So, you can just use:
SELECT S.*
FROM SAMPLE S
ORDER BY (ORDER_PRIORITY < 40) DESC,
FS_GENERATE_DATE IS NOT NULL,
FS_GENERATE_DATE,
ORDER_PRIORITY,
CREATE_ID, CR_DATE, ORDER_QTY;

Select Case is not working with Order by

I was using a simple sql query and getting an ordered list, but when I changed some of the values in the column I'm sorting by, those rows were no longer being sorted correctly.
select distinct u.Email,
case
when l.region_id is null then 'EU'
else l.region_id
end
as Location
from TB_User u
left join cat..location l on l.location=u.Location
where u.Username in (....)
order by l.region_id
I have about 5 rows that returned null for their region_id so they would be at the top of the result set. When I added the case and replaced their value, they still remain at the top. Is there anyway to make these rows sort according to their given value?
You can use CASE also in the ORDER BY. But in this case it seems that you instead want to order by the column which uses the CASE.
ORDER BY Location
If you instead want the null-regions at the bottom:
ORDER BY CASE WHEN l.region_id is null THEN 0 ELSE 1 END DESC,
Location ASC
If your rdbms doesn't support this (like SQL-Server does) you have to repeat it:
ORDER BY CASE WHEN l.region_id IS NULL THEN 'EU' ELSE l.region_id END ASC
You just order by the column value, which is null.
If you want to order by the case statement, just copy it in the order by clause:
order by
case
when l.region_id is null then 'EU'
else l.region_id end
If you are using SQL, try within the SELECT statement, use:
ISNULL(l.region_id, 'EU') AS Location
and then
ORDER BY 2
This will make your query:
SELECT DISTINCT u.Email, ISNULL(l.region_id, 'EU') AS Location
FROM TB_User u
LEFT JOIN cat..location l ON l.location=u.Location
WHERE u.Username in (....)
ORDER BY 2

ORA-00936: missing expression in order by case

Hello i have problem with missing expression in this code:
create or replace view tabulkaliga as
select t.nazov, zs.zapasov, zs.vyhier*nvl(tr.bodyvyhra,0)+zs.remiz*nvl(tr.bodyremiza,0) body, zs.vyhier,zs.remiz,zs.prehier,sl.dali,sl.dostali from
tim t join
ligatim lt on t.id=lt.tim join
liga l on lt.liga=l.id join
sport s on l.sport=s.id join
trvanie tr on s.trvanie=tr.id inner join
zapasstat zs on zs.liga=l.id and t.id=zs.tim inner join
skoreliga sl on sl.liga=l.id and sl.tim=t.id
order by
case
when tr.bodyvyhra is null then (zs.vyhier+0.5*zs.remiz)/ZS.ZAPASOV desc,ZS.VYHIER desc
else body desc, zs.vyhier desc, sl.dali desc
end;
please help, thanks
The desc and asc keywords are not allowed in the case. Plus, only one column at a time is allowed. You can try this:
order by (case when tr.bodyvyhra is null then (zs.vyhier+0.5*zs.remiz)/ZS.ZAPASOV else body end) desc,
ZS.VYHIER desc,
sl.dali desc
Note that this assumes that body is of a numeric type.
Replace
order by
case
when tr.bodyvyhra is null then (zs.vyhier+0.5*zs.remiz)/ZS.ZAPASOV desc,ZS.VYHIER desc
else body desc, zs.vyhier desc, sl.dali desc
end;
with
order by
case when tr.bodyvyhra is null then (zs.vyhier+0.5*zs.remiz)/ZS.ZAPASOV end desc,
case when tr.bodyvyhra is not null then body end desc,
ZS.VYHIER end desc,
case when tr.bodyvyhra is not null then sl.dali end desc;
I split your order by in several cases because:
1) case can contain only expressions (or conditons)
2) expressions must be in one "type group"

How to retrieve list from SQL alphabetically except one item?

I have a table with a column for Occupations in a table in SQL. Along with a few occupations, I also have an item for 'others'. I will display these in a dropdownlist and on selecting 'others' I will show a text box.
Ex: Business
Student
Employee
Others
I want to retrieve the list in Ascending Order, but when I do that using ORDER BY ASC 'Others' will come somewhere in between. I want it to be at the end of the list being returned.
Like Business
Employee
Student
Other
Please suggest a solution to keep the Others at the end of list while retrieving.
(Note: The list is dynamic and will be changing , so I cannot hardcode)
Try something like
SELECT * FROM
(
SELECT *,
CASE WHEN <YourValue> = 'Others' THEN 1 ELSE 0 END AS PrimaryOrder
FROM <YourTable>
) AS SubQuery01
ORDER BY PrimaryOrder, YourOrderByCriteria
A possible solution:
select occupations from mytable where occupations != 'Others' order by 1 ASC
union
select occupations from mytable where occupations == 'Others';
but it's overkill, you can add others by hand
I have tried out a solution and is working for me..
SELECT O.NAME FROM OCCUPATIONTABLE O
ORDER BY CASE
WHEN O.NAME = 'Others'
THEN 1
ELSE 0
END ASC
,O.NAME ASC

SQL: Order first by table.column = 'matchingstring', then order everything else alphabetically?

I'm currently using MySQL and I want to order my book_versions records where the book_versions.name = 'paperback' show up first, and then the rest of the book_versions (book_versions.name != 'paperback') show. How would I accomplish this?
order by case when book_versions.name = 'paperback' then 0 else 1 end,
book_versions.name, -- remove this line if other names should not be ordered
book_versions.isbn
See sqlFiddle to see the difference
in mysql, you can also use field
order by field(book_versions.name, 'paperback') DESC,
book_versions.name ASC,
book_versions.isbn ASC
Try:
ORDER BY
CASE WHEN book_versions.name = 'paperback' THEN 0 ELSE 1 END, -- puts paperbacks first (because paperbacks cause this to =0, which is ordered before 1)
book_versions.name -- then order alphabetically