locked
How to sort rows based on status column RRS feed

  • Question

  • User1760015249 posted

    Hi Friends,

    I have a table with 3 columns, which shows servicing status.

    Product                Price               Status

    Maruthi               1500             Pending

    benz                    500              completed

    Audi                   2000              In Progress

    RR                      1500              Service Fault

    toyoto                 1800             In Progress

    I need to sort the records of status column in Completed,Inprogress, pending, servicefault order.

    We cannot create any other new column.

     

    Friday, August 5, 2016 5:58 PM

Answers

  • User2103319870 posted

    I need to sort the records of status column in Completed,Inprogress, pending, servicefault order.

    You can try with the below code

    SELECT * FROM SampleTable -- Change your table name here
    ORDER BY case WHEN [Status] = 'Completed' THEN 1
                  WHEN [Status] = 'In Progress' THEN 2
                  WHEN [Status] = 'Pending' THEN 3
                  WHEN [Status] = 'Service Fault' THEN 4
             END ASC

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 5, 2016 7:27 PM

All replies

  • User2103319870 posted

    I need to sort the records of status column in Completed,Inprogress, pending, servicefault order.

    You can try with the below code

    SELECT * FROM SampleTable -- Change your table name here
    ORDER BY case WHEN [Status] = 'Completed' THEN 1
                  WHEN [Status] = 'In Progress' THEN 2
                  WHEN [Status] = 'Pending' THEN 3
                  WHEN [Status] = 'Service Fault' THEN 4
             END ASC

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 5, 2016 7:27 PM
  • User77042963 posted
    Select Product,Price,[Status] 
    from yourtable
    
    Order by 
    Case when [Status]='Completed' then 1
    when [Status]='In Progress' then 2
    when [Status]='pending' then 3
    when [Status]='Service Fault' then 4
    Else 9 
    End



    Friday, August 5, 2016 7:27 PM
  • User-1716253493 posted

    Sugest you to change the status column to integer

    set 1 for Completed value

    set 2 for Inprogress value

    set 3 as pending value

    set 4 as service fault value

    you can also create status table containing status code and the description

    Saturday, August 6, 2016 3:29 AM