我有一张这样的数据表。
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
现在,我希望设备 6 和 7 的记录始终位于记录集的顶部,并按创建日期的降序排列。并且记录除 6 和 7 以外的设备,按类型和创建日期在设备类型 6 和 7 的记录之后降序排列。
所以想要的结果如下:
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
我使用了如下查询:
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
但它不起作用,因为它在整个记录集上应用 order by 子句。
有人可以帮我解决这个问题吗?