Sql Order by on Multiple Column

SQL multiple column ordering

ORDER BY column1 DESC, column2

This sorts everything by column1 (descending) first, and then by column2 (ascending, which is the default) whenever the column1 fields for two or more rows are equal.

How to order by multiple columns in sql

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

Order by multiple columns in SQL Server

You can use CASE EXPRESSION for conditional ordering:

SELECT * FROM Table
ORDER BY CASE WHEN @SortOrder = 1 then c1
WHEN @SortOrder = 2 then c2
ELSE c3
END,
CASE WHEN @SortOrder = 1 then c2
ELSE c1
END,
CASE WHEN @SortOrder in(1,2) then c3
ELSE c2
END

SQL order by multiple columns

If I understand correctly, you want to order by the minimum date per title group ascending, followed by the Active column descending. We can use:

SELECT Title, Date, Active
FROM yourTable
ORDER BY MIN(Date) OVER (PARTITION BY Title), Active DESC;

How do SQL order-by with multiple-columns work?

It orders by Country, but if some rows have the same Country, it orders them by CustomerName.

MySQL Sort by multiple columns (3) with some specific rules

In MySQL 5.x

SELECT
yourTable.*
FROM
yourTable
INNER JOIN
(
SELECT
client,
MIN(id) AS min_id,
MIN(Status) AS min_status
FROM
yourTable
GROUP BY
client
)
AS client
ON client.client = yourTable.client
ORDER BY
client.min_status,
client.min_id,
yourTable.calledAt

Conditional Order By in sql server with multiple order columns

I think this is what you want:

ORDER BY
CASE WHEN price IS NULL THEN name END DESC,
CASE WHEN price IS NOT NULL THEN price END DESC,
CASE WHEN price IS NULL THEN age END DESC,
CASE WHEN price IS NOT NULL THEN name END DESC;

Appreciate that when price is NULL, the above reduces to:

ORDER BY
name DESC,
NULL DESC,
age DESC,
NULL DESC;

That is, the alternate CASE expressions just collapse to NULL, leaving the above equivalent to:

ORDER BY
name DESC,
age DESC;

SQL server query, sort on multiple columns

If your problem is that sequence field in other table rather than relation table, then why do not you join them before running recursion? But it likely will be slower than your initial query. Here's a sample

with cte as (
select
r.SourceEntityId, r.TargetEntityId, t.SequenceId, 0 k
from
Relation r
join (
select * from TaskTable1
union all
select * from TaskTable2
) t on r.TargetEntityId = t.id

---------------------------------------
union all select * from cte where k = 1
---------------------------------------
)
, rcte as (
select
SourceEntityId, TargetEntityId, ParentTask = cast(null as uniqueidentifier)
, SequenceId, rn = cast(row_number() over (order by SequenceId) as varchar(8000)), 1 step
from
cte
where
SourceEntityId = 'DAB00C89-961C-84DD-BB43-CFFD18E63594'
union all
select
a.TargetEntityId, b.TargetEntityId, a.SourceEntityId, b.SequenceId
, cast(concat(a.rn, '.', row_number() over (partition by b.SourceEntityId order by b.SequenceId)) as varchar(8000))
, step + 1
from
rcte a
join cte b on a.TargetEntityId = b.SourceEntityId
)
select
*
from
rcte
order by rn

I have not included your X column, I can not get what are trying to calculate. Also, in your expected output values of ParentTask and ParentTask_Id are same. Should be so?

Postgresql group by multiple columns and sort within each group

Use FIRST_VALUE() window function:

SELECT DISTINCT order_id, 
FIRST_VALUE(doc_id) OVER (
PARTITION BY order_id
ORDER BY amount DESC NULLS LAST, rating DESC NULLS LAST, percent DESC NULLS LAST, customer_id
) doc_id
FROM orders;

See the demo.

Order by multiple columns in hibernate criteria

Try changing your query to predicates and add required sort order.

https://www.javacodegeeks.com/2013/04/jpa-2-0-criteria-query-with-hibernate.html

How to order result of hibernate based on a specific order



Related Topics



Leave a reply



Submit