语法如下:
select * from (
select *, row_number() over(order by a.createtime desc ) as rownumber from table_name as a
) as b
where rownumber between 1 and 5
实例如下
select * from
(
select row_number()over(order by dwhybh ) as rowno,* from c60dwyhxx
) as t
where rowno between 4 and 8
and t.dwjc like '%%'
and t.dwqc like '%%'
所用数据表为:
create table [dbo].[c60dwyhxx](
[dwhybh] [varchar](20) not null primary key ,
[dwjc] [varchar](20) not null,
[dwqc] [varchar](60) not null,
[dwlb] [int] not null,
[frdb] [dbo].[xm] null,
[frdbzsh] [varchar](30) null,
[lxr] [dbo].[xm] null,
[lxrmobile] [dbo].[mobile] null,
[qyemail] [varchar](50) null,
[dwlandline] [varchar](15) null,
[qywzdz] [varchar](40) null,
[yhmm] [char](32) null,
[djrq] [date] null,
[uniqueid] [int] not null,
[wtag] [int] null,
[strtag] [varchar](255) null,
[recversion] [bigint] null,
[yhid] [uniqueidentifier] null
)
第二部分:自己写的拼接有省市县三级拼接的sql语句
select row_number()over ( order by hybh) as rowno ,b.xm
,(
select xzqhwm from z21xzqh e where e.xzqh in (select substring(f.xzqh,0,3)+'0000' from c02yhjbxx f
left join z21xzqh d on f.xzqh=d.xzqh
where f.xzqh=d.xzqh and f.yhid=a.yhid )) as fir
,(
select xzqhwm from z21xzqh e where e.xzqh in (select substring(f.xzqh,0,5)+'00' from c02yhjbxx f
left join z21xzqh d on f.xzqh=d.xzqh
where f.xzqh=d.xzqh and f.yhid=a.yhid )) as sec
,(
select xzqhwm
from c02yhjbxx c
left join z21xzqh d on c.xzqh=d.xzqh
where c.xzqh=d.xzqh and c.yhid=a.yhid
) as thir
,b.xzqh,a.hybh,a.yhid,a.uniqueid
from c70hygr a left join c02yhjbxx b on a.yhid=b.yhid
where 1=1
--select substring(xzqh,0,3)+'0000' from c02yhjbxx
--select xzqh from c02yhjbxx
查询结果如下图所示
子查询里嵌套了in关键字。嘻嘻。效率应该不高。但是省的写代码拼接datatable了
