您好,欢迎来到化拓教育网。
搜索
您的当前位置:首页查询sql语句

查询sql语句

来源:化拓教育网


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<=2

第二种方法为:

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

,虚拟主机,虚拟主机,虚拟主机

Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务