22,209
社区成员
发帖
与我相关
我的任务
分享
drop table #table1
create table #table1(id int, name varchar(20))
insert #table1 values(1, 's')
insert #table1 values(2, 'c')
insert #table1 values(3, 'b')
insert #table1 values(4, 'd')
insert #table1 values(6, 's')
insert #table1 values(8, 't')
insert #table1 values(9, 'a')
insert #table1 values(10, 'cc')
insert #table1 values(11, 'tt')
insert #table1 values(13, 'ss')
insert #table1 values(14, 'e')
insert #table1 values(15, 'b')
insert #table1 values(16, 'u')
insert #table1 values(17, '9')
insert #table1 values(18, 'jj')
insert #table1 values(19, 'pp')
insert #table1 values(20, 'to')
insert #table1 values(21, 'tb')
insert #table1 values(22, 'tu')
insert #table1 values(23, 'uc')
insert #table1 values(24, 'ue')
insert #table1 values(25, 'ub')
insert #table1 values(26, 'yk')
drop table #
select (ROW_NUMBER() over(order by id)-1)/10 as rowNo, id, name into # from #table1
select * from # a where id=(select max(id) from # where rowNo=a.rowNo)
select px=identity(int,1,1),* into #t from tb
select * from #t where px%10=0 or not exists(select 1 from #t where px>t.px)
with cte as
(select no=row_number(order by id),* from tb)
select * from cte where no%10=0
union
select top 1 * from cte order by no desc
;WITH CTE AS (
SELECT * ,ROW_NUMBER() OVER (ORDER BY ID) AS NUM
FROM TABLE1
WHERE ...
)
SELECT ID FROM CTE A
WHERE NUM % 10 = 0
OR NOT EXISTS (
SELECT 1 FROM CTE WHERE NUM > A.NUM
)
用CTE排序 除10余数为0的就满足要求了
select px=identity(int,1,1),* into #t from tb
select * from #t where px%10=0
select * from
(select no=row_number(order by id),* from tb)t
where no%10=0
;WITH CTE AS (
SELECT * ,ROW_NUMBER() OVER (ORDER BY ID) AS NUM
FROM TABLE1
WHERE ...
)
SELECT ID FROM CTE
WHERE NUM % 10 = 0