Custom sorting in sql query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pb2000
    New Member
    • Apr 2010
    • 13

    Custom sorting in sql query

    Hello,
    I learn SQL SERVER.

    I wrote:

    SELECT
    "All"
    UNION SELECT DISTINCT
    KindsColumn From ProductsTable

    I obtain:
    ----------------------
    1. AA_kind
    2. AB_kind
    3. All
    4. B_kind
    5. Z_kind

    But i take it into DropDownList where 'All' should be at the top.

    I would like to obtain (where 2-5 are sorted)
    ---------------------------
    1. All
    2. AA_kind
    3. AB_kind
    4. B_kind
    5. Z_kind

    Thank You fot the answer. It will be nice lesson for me.
  • robjens
    New Member
    • Apr 2010
    • 37

    #2
    Ah. I had to see for a bit what you wanted exactly. Thought at first you wanted capital and small case letters sorted in a certain sequence. This kind of "sorting" you won't get the result though. I assume you'd want "All" as the value of a ComboBox/dropdown list to have it run a function lateron perhaps? Else I don't see any reason you'd have that value "All" in there, just add it later to a control using .Add method. If it's pure database we are talking here, you will need to add another field integer type and sort on that like All (1) AA_kind (2) etc. Then just hide the column. You'd still be able to sort on the ABC just enforce All to the top.

    Comment

    • mafaisal
      New Member
      • Sep 2007
      • 142

      #3
      Originally posted by pb2000
      Hello,
      I learn SQL SERVER.

      I wrote:

      SELECT
      "All"
      UNION SELECT DISTINCT
      KindsColumn From ProductsTable

      I obtain:
      ----------------------
      1. AA_kind
      2. AB_kind
      3. All
      4. B_kind
      5. Z_kind

      But i take it into DropDownList where 'All' should be at the top.

      I would like to obtain (where 2-5 are sorted)
      ---------------------------
      1. All
      2. AA_kind
      3. AB_kind
      4. B_kind
      5. Z_kind

      Thank You fot the answer. It will be nice lesson for me.
      That Means,

      Code:
      Select fld From (
      Select 1 Flag,'All' Fld Union All Select 2 Flag, KindsColumn fld 
      From ProductsTable ) a 
      Order by Flag,Fld
      This may be help you

      Faisal M A

      Comment

      • ck9663
        Recognized Expert Specialist
        • Jun 2007
        • 2878

        #4
        Try this:

        Code:
        SELECT 0 as sortorder,
        "All"
        UNION SELECT DISTINCT 1 as sortorder
        KindsColumn From ProductsTable
        order by 1, 2
        Happy Coding!!!

        ~~ CK

        Comment

        Working...