Order By Statement Within DataSet Query

  • I know there's several ways to sort data when multiple fields are used. However, I'm taking a different path that I'm not used to.

    I'm wokring on a report that gives the user the selection of two different sorts. In these two sorts, each sort is sorted with multiple fields. To know what I'm talking about, here's the Order By I'm trying to use and with this, I'm getting an error and dont understand why. Hope someone here can assist.

    Thanks.

    ORDER BY CASE

    WHEN @SortBy = 'A' THEN [CASE ID], [CONTACT ALPHA], [SCHEDULED PRIORITY], [CONTACT ALPHA]

    WHEN @SortBy = 'P' THEN [CASE ID], [SCHEDULED PRIORITY], [CONTACT ALPHA]

    END

  • Hi,

    Don't think you can reference multiple fields in one case statement - you'll need a case statement per order by field:

    ORDER BY [CASE ID],

    CASE

    WHEN @SortBy = 'A' THEN [CONTACT ALPHA]

    WHEN @SortBy = 'P' THEN [SCHEDULED PRIORITY]

    END,

    CASE

    WHEN @SortBy = 'A' THEN [SCHEDULED PRIORITY]

    WHEN @SortBy = 'P' THEN [CONTACT ALPHA]

    END,

    etc

    Cheers

  • Thanks. That worked. I knew I was over thinking it.

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

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