prameela rani
prameela rani

Reputation: 223

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.

Upvotes: 1

Views: 44

Answers (2)

SMA
SMA

Reputation: 37103

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.

Upvotes: 4

Andrew LaPrise
Andrew LaPrise

Reputation: 3423

SELECT *
FROM t
ORDER BY CustomerID,
         DisplayOrder;

Upvotes: 3

Related Questions