• 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

    __________________________________________________________________________________________________________
    How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/