首頁  >  文章  >  資料庫  >  查询sql语句

查询sql语句

WBOY
WBOY原創
2016-06-07 17:39:35947瀏覽

usedb go createtabletablel ( idintidentity(1,1)notnull, propertyvarchar(20)notnull, datedatetimenotnull ) go insertintotablelvalues('CSD001','2012-12-12:12:10') insertintotablelvalues('CSD001','2012-12-12:12:13') insertintotablelvalues('CS

use db

go

 

create table tablel

(

 id int identity(1,1) not null,

 property varchar(20) not null,

 date datetime not null

)

go

 

insert into tablel values('CSD001','2012-12-12:12:10')

insert into tablel  values('CSD001','2012-12-12:12:13')

insert into tablel  values('CSD001','2012-12-12:12:14')

insert into tablel values('CSD001','2012-12-12:12:16')

 

insert into tablel values('CSD002','2012-12-12 12:13:14')

insert into tablel values('CSD002','2012-12-12 12:13:16')

insert into tablel values('CSD002','2012-12-12 12:13:18')

insert into tablel values('CSD002','2012-12-12 12:13:45)

 

insert into tablel values('CSD003','2012-12-12 12:13:14')

insert into tablel values('CSD003','2012-12-12 12:13:16')

insert into tablel values('CSD003','2012-12-12 12:13:18')

insert into tablel values('CSD003','2012-12-12 12:13:45)

 

insert into tablel values('CSD004,'2012-12-12 12:13:14')

insert into tablel values('CSD004','2012-12-12 12:13:16')

insert into tablel values('CSD004','2012-12-12 12:13:18')

insert into tablel values('CSD004','2012-12-12 12:13:45)

 

查询每个属性最小两个值;

 第一种方法为:

Select * from (select row_number() over(patittion  by property order by a.date asc) as rowIndex, * from tablel as a where property in ( select distict(property)  from tablel )) t where rowIndex

 

 第二种方法为:

       或者 select * from tablel as a where property in (select top 2 Property from tablel  where a.property=property order by a.date asc)

,香港虚拟主机,香港虚拟主机,香港虚拟主机
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn