我有一个包含此类数据的表。对单个查询使用带有ORDER BY的UNION子句
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
但它没有工作,因为它是在整个记录集上应用子句。
有人可以帮我吗?
当您使用'UNION'将结果合并成使用两个查询一个表解决了这个问题我自己'ORDER BY'条款并不完全对工作结果。 – Murtaza 2012-04-23 04:50:43