locked
I want to retrieve Data in an Order from a SQL table? RRS feed

  • Question

  • I have a table that contains a students result. Now I want to retrive this  data in an order so it can display in an effective manner. 

    The result is in the format of Grades. 

    eg. Grade : A+ , A , B+, B, C+, C

    How can I write a query so I can retrive data in the format : A+,  A,  B+,  B,  C+, C etc..

    Lets say the table contains Student Name, Course Subject, Year, Grades. 

    • Moved by Tom Phillips Thursday, January 12, 2012 2:29 PM TSQL question (From:SQL Server Database Engine)
    Thursday, January 12, 2012 1:52 AM

Answers

  • Assuming the Grade can be A-, I assumed we wanted to show in this order

    A +

    A

    A-

    B+

    B

    B-

    etc.

    So, + will be 1, nothing will be 2 and - will be 3 in order, this is what I used in my case expression.

    BTW, I haven't tested it, but I believe it should work. BTW, you're right, there was a typo in my expression, so I corrected it now.

     


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    • Edited by Naomi N Thursday, January 12, 2012 2:33 AM
    • Proposed as answer by Abhay_78 Thursday, January 12, 2012 4:50 AM
    • Marked as answer by Shavendra Chand Thursday, January 12, 2012 7:02 PM
    Thursday, January 12, 2012 2:31 AM

All replies

  • select * from Graders ORDER BY LEFT(Grade,1), case  right(Grade,1) 
    
    when  '+' then 1 when '-' then 3 else 2 end

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    • Edited by Naomi N Thursday, January 12, 2012 2:32 AM
    Thursday, January 12, 2012 2:07 AM
  • Assuming the Grade can be A-, I assumed we wanted to show in this order

    A +

    A

    A-

    B+

    B

    B-

    etc.

    So, + will be 1, nothing will be 2 and - will be 3 in order, this is what I used in my case expression.

    BTW, I haven't tested it, but I believe it should work. BTW, you're right, there was a typo in my expression, so I corrected it now.

     


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    • Edited by Naomi N Thursday, January 12, 2012 2:33 AM
    • Proposed as answer by Abhay_78 Thursday, January 12, 2012 4:50 AM
    • Marked as answer by Shavendra Chand Thursday, January 12, 2012 7:02 PM
    Thursday, January 12, 2012 2:31 AM
  • Hi Naomi! 

     

    Thanks so much for taking your time to answer my question and explaining further. Appreciate sooo much! :)

     

    Thanks once again. 

     

    Regards,

    Shavendra

     

    Thursday, January 12, 2012 10:21 PM