user1187
user1187

Reputation: 2198

how to writea a select query based on ranks?

i have one table

table1

id   city    projects       Rank
----------------------------------
1    che        p2           2
2    bang       p1           1
3    che        p4           1
4    bang       p3           2
5    bang       p5           3
6    gur        p6           1
7    gur        p7           2

based on rank i want to select city and project i want like this

   bang       p1            1
    che        p4           1
    gur        p6           1
    che        p2           2
    bang       p3           2
    gur        p7           2
    bang       p5           3

select * from table1 where Rank = ?

Upvotes: 0

Views: 87

Answers (6)

Robert
Robert

Reputation: 25753

I hope this will help you:

select city, projects, Rank from table1 
order by Rank, city, projects

you can also use positions in order by clause:

select city, projects, Rank from table1 
order by 3,1,2

Solution with 0 on the bottom:

 select city, projects, Rank from table1 
    order by
    case 
     when Rank>0 then 1
     else 2
    end,
    Rank, city, projects

Upvotes: 1

arkascha
arkascha

Reputation: 42915

Many answers here are fine.

However it is common practice to use UPPERCASE for language constructs in SQL:

SELECT city,project,rank FROM table1 ORDER BY rank,project;

:-)

Upvotes: 1

Drew
Drew

Reputation: 24959

select city,project,rank from table1 order by rank,project,city

Upvotes: 2

Srinivas B
Srinivas B

Reputation: 1852

try this...

select city,projects,rank from table1 group by rank order by rank,city,projects;

Upvotes: 0

Mudassir Hasan
Mudassir Hasan

Reputation: 28751

use ORDER BY clause in your query.

SELECT city, projects, rank FROM table1 
order by rank

Upvotes: 1

SRIRAM
SRIRAM

Reputation: 1888

try this

 select city, projects, Rank from table order by Rank

Upvotes: 1

Related Questions