locked
Problem with T SQL RRS feed

  • Question

  •  

    select ItemGroupDefinitionID,Description from
    (
    Select 'ALL' as ItemGroupDefinitionID,'DISPLAY ALL PRODUCTS' as Description

    union

    SELECT cast(A.ItemGroupDefinitionID as varchar),Description FROM [ItemGroupDefinition] A

    )B

    ORDER BY  CASE WHEN ItemGroupDefinitionID='ALL' THEN 0 ELSE ItemGroupDefinitionID END

    I want the result set to be sorted according to the Description  rather than Ids. The condition is such that the first row remains unchanged because I need to display all the products , the sort has to be done from the second row based on the description. 

     result set:

    ItemGroupDefinitionID Description
    ALL    DISPLAY ALL PRODUCTS
    2       Network Integration
    3       Network Drops
    5      CH Square Feet
    6      FH Square Feet
    8      EA CH Square Feet
    9      EA CH LBand Square Feet

    Thursday, October 31, 2013 10:02 AM

Answers

  • select ItemGroupDefinitionID,Description from
    (
    Select 'ALL' as ItemGroupDefinitionID,'DISPLAY ALL PRODUCTS' as Description,0 as SortId

    union

    SELECT cast(A.ItemGroupDefinitionID as varchar),Description FROM [ItemGroupDefinition] A, as SortId

    )B

    ORDER BY SortId ASC,Description ASC


    Sandeep J. Sharma (eZee Technosys)


    Thursday, October 31, 2013 10:12 AM

All replies

  • select ItemGroupDefinitionID,Description from
    (
    Select 'ALL' as ItemGroupDefinitionID,'DISPLAY ALL PRODUCTS' as Description,0 as SortId

    union

    SELECT cast(A.ItemGroupDefinitionID as varchar),Description FROM [ItemGroupDefinition] A, as SortId

    )B

    ORDER BY SortId ASC,Description ASC


    Sandeep J. Sharma (eZee Technosys)


    Thursday, October 31, 2013 10:12 AM
  • Hey Sandeep,

    Many Thanks !

    It worked

    Ajit

    Thursday, October 31, 2013 10:23 AM
  • You can add another sort column Description to your original code to make it work.

    You should use UNION ALL instead of UNION in your code.

    Thursday, October 31, 2013 4:53 PM