• LinksUp (8/1/2015)


    If the ordering is indeed the alter_msg, then I would create another table to define the sorting method. Hard coding each possibility could quickly become a nightmare! :w00t:

    Here is a solution with an additional table thrown in:

    DECLARE @Table TABLE

    (minv_code INT,

    alert_msg varchar(10),

    alert_time Datetime)

    DECLARE @SortOrder TABLE

    (

    Sort_id int,

    alert_msg varchar(10)

    )

    INSERT INTO @Table VALUES

    (873939, 'Reverse', '7/24/2015 3:31:18'),

    (873939, 'Tamper', '7/24/2015 3:30:00'),

    (873939, 'Meter', '7/24/2015 3:31:22'),

    (873940, 'Reverse', '7/24/2015 3:30:00'),

    (873940, 'Tamper', '7/24/2015 3:31:22')

    INSERT INTO @SortOrder VALUES

    (1, 'Meter'),

    (2, 'Tamper'),

    (3, 'Reverse')

    select *

    from @Table t

    inner join @SortOrder s on s.alert_msg = t.alert_msg

    order by t.minv_code, s.sort_id

    +1000 to that.

    --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)