prameela rani prameela rani - 4 months ago 9
SQL Question

Order rows based on different column value

I have data in table in following format.

CustomerID | CustomerName | DisplayOrder | isActive
1 | Test | 1 | 1
1 | Test | 2 | 1
2 | Test2 | 1 | 1
1 | Test | 3 | 1
2 | Test2 | 2 | 1


Above table need to be sorted based on displayorder column per customer. Each customer will have thier own display order. Here I cannot use OrderBy Clause direcly because Display order will repear per customer.

Expected Output is:

CustomerID | CustomerName | DisplayOrder | isActive
1 | Test | 1 | 1
1 | Test | 2 | 1
1 | Test | 3 | 1
2 | Test2 | 1 | 1
2 | Test2 | 2 | 1


Please help on order by clause to sort based on customer.

SMA SMA
Answer

You just need to sort by customerId and then display order (if customer id is same) like:

SELECT * 
FROM mytable 
ORDER BY customerId, displayOrder;

Fiddle for the same.