• cms9651 (9/6/2012)


    The rule for the sorting is mat geographic NAME.

    MAW = My Area West

    MAE = My Area East

    MAC = My Area Center

    MAS = My Area South

    Oh, why we couldn't see that from your first post. It is so obvious ... (yes it's a sarcasm) :hehe:

    If you understand how I've made 'Tot' to appear as a last one, you should be able to figure out how to force the order based on the above requirement. You will need to hard-code priority, or create and use table which list your Geographic NAMEs where you can maintain the order sequence.

    1. Just hard-coded order priority

    SELECT

    [MAT],

    [DVD],

    [SALES]

    FROM

    TestTable

    ORDER BY DVD DESC, CASE MAT WHEN 'MAW' THEN 1

    WHEN 'MAE' THEN 2

    WHEN 'MAC' THEN 3

    WHEN 'MAS' THEN 4

    ELSE 5 --'Tot'

    END

    2. With order sequence maintained in dedicated table

    CREATE TABLE dbo.RefGeographic (Code CHAR(3), Description VARCHAR(50), OrderSequence INT)

    INSERT dbo.RefGeographic SELECT 'MAW','My Area West',1

    INSERT dbo.RefGeographic SELECT 'MAE','My Area East',2

    INSERT dbo.RefGeographic SELECT 'MAC','My Area Center',3

    INSERT dbo.RefGeographic SELECT 'MAS','My Area South',4

    INSERT dbo.RefGeographic SELECT 'MAN','My Area North',5

    -- now you can use it in your query

    SELECT t.[MAT],

    t.[DVD],

    t.[SALES]

    FROM TestTable t

    LEFT JOIN RefGeographic g

    ON g.Code = t.MAT

    ORDER BY DVD DESC, ISNULL(g.OrderSequence, 9999999)

    You can see that a second way will allow you to change the order (if it will be ever required) without code change. Also it will work great in case if new geographic names will need to be added (eg. "My Area South-West" and "My Area Middle Of Nowhere"):hehe:

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]