DBAsupport.com Forums - Powered by vBulletin
Results 1 to 6 of 6

Thread: Ordering an alphanumeric resultset.

  1. #1
    Join Date
    Feb 2004
    Posts
    77

    Ordering an alphanumeric resultset.

    Hi,

    I have a resultset which looks like below.

    A
    B
    C
    D
    E
    F
    G
    H


    I would like to order this result set with reference to another string by the dictinary distance. So taking D as reference, I would like to see the following output

    D
    C
    E
    A
    F
    G
    H

    This is a simple example. Actually the resultset I am retreiving can have variable length string. So the solution should be scalable to

    Thanks in advance.

  2. #2
    Join Date
    Dec 2000
    Location
    Ljubljana, Slovenia
    Posts
    4,439
    I'm totaly confused. Can you explain a bit further the logic behind the wanted sorted result? Your example doesn't make any sense to me. And btw, where have letter 'B' dissapeared to in your sorted result?
    Jurij Modic
    ASCII a stupid question, get a stupid ANSI
    24 hours in a day .... 24 beer in a case .... coincidence?

  3. #3
    Join Date
    Feb 2004
    Posts
    77
    The letter B should be there in the result set. Sorry about that.

    Here is the problem I am trying to solve. I have a table called TASKS.

    TASK, LOCATION, PRIORITY

    The data in it looks something like this.
    TASK1 112A1 1
    TASK2 112B1 1
    TASK3 223C2 1
    TASK4 334D4 1
    TASK5 445E5 1
    TASK6 113A1 2
    TASK7 223D3 2
    TASK8 446E7 2

    If a user is dong TASK4, he will be at Location 334D4. After completion of TASK4, I want to display the next task based on Priority and the location closest to him. In this case it would be either 223C2 or 445E5.

    The reason for doing something like this is that the user is already in a particular location, so give him his next task in the vicinity only.

    It has to be done in one SQL only. Since I am changigna third party software, so I can afford to only change the SQL taht fetches the next task. Beyond that I don't know the processing logic to make any procedural changes.

    Thanks.

  4. #4
    Join Date
    Nov 2002
    Location
    Geneva Switzerland
    Posts
    3,142
    You should be able to devise a numerical metric lbased on something like:
    10000*to_number(substr(location,1,3)) + 10*ascii(substr(location,4,1)) + to_number(substr(location,5,1))

    BUT the details depend on how those crummy codes are to be interpreted:
    If you're at 445E5, which is nearer 446E5 or 445F5?

    P.S. better solution (but not just one SQL): create a lookup table to convert the codes to (x,y) coordinates and create a function to determine the distance between to location codes.
    Last edited by DaPi; 08-20-2004 at 03:21 AM.

  5. #5
    Join Date
    Dec 2000
    Location
    Ljubljana, Slovenia
    Posts
    4,439
    Now, after your explanation, it makes perfect sense.

    I would use LEAD() and LAG() analytical functions. As I understand, for each task you basicaly need to display the closest two tasks (one sorted before it and one after it) with respect to priority and location. So, does the following gives you what you need?
    Code:
    SELECT task, location, priority,
           lead(task) OVER(ORDER BY priority, location) next_task_a,
           lead(location) OVER(ORDER BY priority, location) next_location_a,
           lead(priority) OVER(ORDER BY priority, location) next_priority_a,
           lag(task) OVER(ORDER BY priority, location) next_task_b,
           lag(location) OVER(ORDER BY priority, location) next_location_b,
           lag(priority) OVER(ORDER BY priority, location) next_priority_b
    FROM tasks;
    Jurij Modic
    ASCII a stupid question, get a stupid ANSI
    24 hours in a day .... 24 beer in a case .... coincidence?

  6. #6
    Join Date
    Feb 2004
    Posts
    77
    Thanks jmodic and DaPi. I will try both the solutions and see which one works out with minimal code changes.

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