日期:2014-05-17 浏览次数:20599 次
declare @t table (id int identity(1,1),d date,t varchar(10)); insert into @t select '2012-07-01','aa' union all select '2012-07-01','bb' union all select '2012-07-02','cc' union all select '2012-07-01','dd' union all select '2012-07-20','xx'; select * from ( select rn=ROW_NUMBER() over(partition by d order by id),* from @t ) x where x.rn in (1,2) /* rn id d t -------------------- ----------- ---------- ---------- 1 1 2012-07-01 aa 2 2 2012-07-01 bb 1 3 2012-07-02 cc 1 5 2012-07-20 xx */ --你看是不是要这种,每一天前两条。
------解决方案--------------------
;with cte as( SELECT * FROM xingzheng_daiban_shixiang WHERE (DATEPART(yy, riqi) = '2012') AND (DATEPART(mm, riqi) = '7') ) SELECT id, CONVERT(varchar, DATEPART(dd, riqi)) AS nDay, biaoti,riqi FROM cte T WHERE id IN (SELECT top 2 id FROM cte WHERE riqi=T.riqi order by id) ORDER BY riqi