欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

查询sql语句

程序员文章站 2024-01-27 16:31:52
...

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)

,香港虚拟主机,香港虚拟主机,香港虚拟主机
相关标签: 查询 语句