考慮:SQL Server:如何使用UNION和兩個查詢都有WHERE子句?
select top 2 t1.ID, t1.ReceivedDate
from Table t1
where t1.Type = 'TYPE_1'
order by t1.ReceivedDate desc
而且:需要過濾
兩個查詢
select top 2 t2.ID
from Table t2
where t2.Type = 'TYPE_2'
order by t2.ReceivedDate desc
另外,這些返回ID
了,我就是在尋找:(13,11和12,6)
基本上,我想要兩個特定的最近兩個記錄數據的pes。
我想聯盟這兩個查詢等一起這樣:
select top 2 t1.ID, t2.ReceivedDate
from Table t1
where t1.Type = 'TYPE_1'
order by ReceivedDate desc
union
select top 2 t2.ID
from Table t2
where t2.Type = 'TYPE_2'
order by ReceivedDate desc
問題:
的問題是,這種查詢是無效的,因爲第一select
不能有order by
條款,如果它是正在unioned
。如果沒有order by
,它不能有top 2
。
我該如何解決這種情況?
我想你的意思是,你有'順序''不''哪裏'的麻煩。 – 2011-03-24 23:39:19