How to get order by column containg value

  • Hi

    i have one table like temployedetails

    columns like Empno,EmpName,City,xlsorder

    temployedetails

    ----------------

    Empno EmpName City xlsorder

    ------ --------- ---- --------

    101 scott hyd 1

    102 tiger jum 2

    103 jakir trhsj 3

    104 upers luknow 2

    105 arerewr uwqetr 4

    106 qwer qwerq 3

    107 qewr rtutyu 2

    i want out put is orderby xlsorder is 2 ,3,4,1 (means first where xlsorder is 2 then 3 then 4 then 1).

    i want out put like this

    102 tiger jum 2

    104 upers luknow 2

    107 qewr rtutyu 2

    103 jakir trhsj 3

    106 qwer qwerq 3

    105 arerewr uwqetr 4

    101 scott hyd 1

    How to write a select query , please help me.

    Thanks in advance.

    Regards

    Swamy.

  • DECLARE@Sample TABLE (EmpNo INT, EmpName VARCHAR(20), City VARCHAR(20), xlsOrder INT)

    INSERT@Sample

    SELECT101, 'scott', 'hyd', 1 UNION ALL

    SELECT102, 'tiger', 'jum', 2 UNION ALL

    SELECT103, 'jakir', 'trhsj', 3 UNION ALL

    SELECT104, 'upers', 'luknow', 2 UNION ALL

    SELECT105, 'arerewr', 'uwqetr', 4 UNION ALL

    SELECT106, 'qwer', 'qwerq', 3 UNION ALL

    SELECT107, 'qewr', 'rtutyu', 2

    SELECT*

    FROM@Sample

    ORDER BYCASE xlsOrder

    WHEN 1 THEN 30

    WHEN 2 THEN 0

    WHEN 3 THEN 10

    WHEN 4 THEN 20

    ELSE 100

    END


    N 56°04'39.16"
    E 12°55'05.25"

  • One trick I have used before is to create an integer column called OrderBy. I then populate it with values that will give me the desired order from the query. One nice thing is that it can be dynamic. If, say, the OrderBy column is incremented every time some other value is selected by users, then "select ... order by OrderBy desc" will sort from most often selected to least often selected.

    Tomm Carr
    --
    Version Normal Form -- http://groups.google.com/group/vrdbms

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply