1

I have a table with roll_no & No_of_items (Both not primary)

Is it possible to order them the following way: The first entry is with the highest no of items, then all the entries belonging to that roll_no come, then the next entry from a diff roll_no that has the highest no of items and then all the entries belonging to that client and so on

For Eg.

Column 1 Column 2
A          12
A          05
C          19
C          18
C          02 
B          05

Should display as:

C 19
C 18
C 02
A 12
A 05
B 05
3
  • What kind of SQL? It's much easier in some than others. Commented Oct 17, 2012 at 20:56
  • are those contained in the same column? or different columns? Commented Oct 17, 2012 at 20:58
  • Different Column, Oracle 10g. Thanks Commented Oct 17, 2012 at 21:00

2 Answers 2

1

Try this.

select t1.*
from yourtable t1
    inner join 
    (
        select 
            client_id, max(numberofdays) cnt 
        from yourtable 
        group by client_id
    ) t2
        on t1.client_id = t2.client_id
order by cnt desc, client_id, numberofdays desc;

This may also work, dependant on SQL flavour

select *
from yourtable
order by 
    max(numberofdays) over (partition by client_id) desc, 
    client_id, 
    numberofdays desc;

Edit: added client_id to order to keep rows with same number.

Sign up to request clarification or add additional context in comments.

Comments

1

what you will use in your sql query is 'order by' your code will look like this using MySql

<?php
$query = mysql_query("select `client_id`, `No_of_Days` FROM `tablename` order by `No_of_Days` DESC ");
while($row = mysql_fetch_object($query))
{
     echo $row->client_id." ".$row->No_of_Days;
}
?>

the query is what does the main work here

1 Comment

This doesn't return the order requested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.