I have a table with data like this.
Id PId Device Status Type Created_Date
=== === ====== ====== ==== ============
1 2 1 today High 2012-04-12 08:11:51.583
2 2 4 today Medium 2012-04-02 01:39:52.393
3 3 5 today Medium 2012-04-02 01:10:02.443
4 2 6 today High 2012-04-02 01:05:25.063
5 2 3 today High 2012-04-02 01:03:08.360
6 2 7 today High 2012-04-02 01:02:57.093
7 2 2 today High 2012-04-02 00:22:37.807
Now, I want the records of Device 6 and 7 always on the top of the record set with descending order of created date. And records with device except 6 and 7 order by Type and Created Date Descending after the records of device type 6 and 7.
So the desired result is like below :
Id PId Device Status Type Created_Date
=== === ====== ====== ==== ============
4 2 6 today High 2012-04-02 01:05:25.063
6 2 7 today High 2012-04-02 01:02:57.093
1 2 1 today High 2012-04-12 08:11:51.583
5 2 3 today High 2012-04-02 01:03:08.360
7 2 2 today High 2012-04-02 00:22:37.807
2 2 4 today Medium 2012-04-02 01:39:52.393
I have used the query as below :
select * from TblAlert where PId=2 and ( Device=6 OR Device=7) and ( Status='Today' or Status=0)
UNION
Select * from TblAlert Where PId=2 and ( Device<>6 OR Device<>7)and (Status='Today' or Status=0)
order by Type,Created_Date desc
but its not working as it is applying order by clause on whole record set.
Can anybody help me regarding this please?
Solved it my self by Using Table variable as