Results 1 to 6 of 6

Thread: Need help with a SQL statement

  1. #1

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Resolved Need help with a SQL statement

    This SQL statement is in a VB prog, so thats why I am posting it here.
    Nothing to do with the fact that most users commute here so I am more likely to get it answered quickly ... honest

    OK then ... I have a statement that grabs a load of records from a table.
    One of the fields is known as "Risk Indicator" and is stored as either "R", "A" or "G", symbolising Red, Amber or Green.

    I want to bring the recordset back in the order of (R)ed, (A)mber, and then (G)reen, which obviously leads to problems as a string sort in either ascending or descending format will not give me what I need.

    My question is ... is there a way of transposing the "R", "A" and "G" values IN the select statement, so they could be something like "A", "B" and "C" ?
    That way I would have my order as I need it.

    I want to do it in the SQL statement if I possibly can, so I don't have to faff about afterwards, sorting my results etc.

    Thanks.
    Last edited by TheBionicOrange; Jul 19th, 2006 at 10:58 AM.

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Need help with a SQL statement

    Give them numeric values, 1 (Red) 2 (Amber) and 3 (Green). Then, in your program, display the color names for those numbers.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  3. #3

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Need help with a SQL statement

    Yes thats kinda my point ... HOW do I do that in a SQL statement ?
    By the way .. I'm using SQL Server 2000 before anyone asks.

  4. #4
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: Need help with a SQL statement

    you can use a CASE expression:
    Code:
    SELECT field1
             , field2
             , the_code, 
             , case the_code 
                      when 'R' then 'A' 
                      when 'A' then 'B' 
                      when 'G' then 'C' 
                      else '?'
                end AS sort_code
    FROM my_table etc.
    ORDER BY 4
    Note that if you want to sort by the transformed value, the easiest way to do it is by using the position of the expression in the ORDER BY clause - otherwise you'd have to specify that whole expression again in the ORDER BY clause (i.e. ORDER BY case the_code when ... etc.) - you CANNOT say ORDER BY sort_code (that would be too easy ..
    "It's cold gin time again ..."

    Check out my website here.

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Need help with a SQL statement

    Belongs in the DB forum

    At any rate - if you are going to be doing this a lot and if you already have a "lookup/description" table that changes the A to Amber - then also add a column to that table for SortOrder - make it an INT field - put a 1, 2 or 3 into it. JOIN to that table and ORDER BY SortOrder.

    That's the answer from the DB-side of the forum...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Need help with a SQL statement

    OK great.

    Thank you both

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width