Sorting One Item first, then the rest

  • If I have a group and sort on Country in my report. I want USA to show up first, and then the rest of the countries to show up in alphabetical order after it.

    I'm having trouble formulating the first sort.

    Ideas?

  • You could use 2 queries with a UNION. First write the query for the 'USA' results, then UNION it with a second query that has criteria excluding the 'USA' results.

    _________________________________
    seth delconte
    http://sqlkeys.com

  • I agree with Seth with some minor changes.

    1. Use UNION ALL... the code will be faster because it doesn't do an implicit DISTINCT like UNION would.

    2. Second, you'll need to add a "calculated column" to both selects so that the final sort will will work out right. The column should be (for example) a "1" in the first SELECT and a "2" the second. Then, sort on that column and the Country.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • I made up an answer for myself. In the Group properties, under Sorting, use the function option. Formula is something like:

    =IIF(Fields!Country.Value = "United States of America", "Aaaaaa", Fields!Country.Value)

    Unless there is a country that comes before Aaaaaaa alphabetically, it should work. Its working for me right now.

    (I was trying to figure a way to use two sorts in order, but it appears that's more for [e.g. Country, then State, then County, then City].

    Thanks for the ideas though. Gave me something to think about for another report I'm working on.

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

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