求解一句SQL语句查询-主键为Guid怎么去当中的记录

时间:2021-07-29 15:09:53
除了用这种
with Incubator_table
as (select row_number() over (order by count(BusinessIncubationId) desc) as rowIndex, UnitName, EnterpriceCode, count(BusinessIncubationId) as CorporationCount from IncubatorRegister
inner join BusinessIncubation on IncubatorRegister.IncubatorId = BusinessIncubation.IncubatorId group by UnitName, EnterpriceCode)
select * from Incubator_table where rowIndex between 11 and 20

没办法用
select top 10 UnitName, EnterpriceCode, count(BusinessIncubationId) as CorporationCount  from IncubatorRegister
inner join BusinessIncubation on IncubatorRegister.IncubatorId = BusinessIncubation.IncubatorId
where IncubatorRegister.IncubatorId not in (select top 20 IncubatorRegister.IncubatorId from IncubatorRegister)
group by UnitName, EnterpriceCode order by CorporationCount desc
这种方式完成吗啊,int型自增长属性貌似可以想问下怎样用第二种方法完成guid主键情况下的取11-20条的记录

7 个解决方案

#1


你的问题不在于如何取, 而在于你怎么理解什么是 top  11-20

#2


select top 10 id from tb where id not in(select top 10 id from tb)

#3


取n到m行

1. 
select top m * from tablename where id not in (select top n id from tablename order by id asc/*|desc*/) 

2. 
select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入到临时表 
set rowcount n   --只取n条结果
select * from 表变量 order by columnname desc 

3. 
select top n * from  
(select top m * from tablename order by columnname) a 
order by columnname desc 


4.如果tablename里没有其他identity列,那么: 
先生成一个序列,存储在一临时表中.
select identity(int) id0,* into #temp from tablename 

取n到m条的语句为: 
select * from #temp where id0 > =n and id0  <= m 

如果你在执行select identity(int) id0,* into #temp from tablename这条语句的时候报错,那是因为你的DB中间的select into/bulkcopy属性没有打开要先执行: 
exec sp_dboption 你的DB名字,'select into/bulkcopy',true 


5.如果表里有identity属性,那么简单: 
select * from tablename where identity_col between n and m  

6.SQL2005开始.可以使用row_number() over()生成行号
;with cte as
(
 select id0=row_number() over(order by id),* from tablename
)
select * from cte where id0 between n to m

#4


引用 1 楼 fcuandy 的回复:
你的问题不在于如何取, 而在于你怎么理解什么是 top 11-20

我的这里是想取出企业的名字,然后按照企业下的子公司数量倒序排列取出其中的11-20条~不知道这个能不能用第二种方法来弄~求解答~

#5


建立表变量create @t table(id int identity(1,1),b int)
你的数据插入表变量
从表变量中按照id取出你需要的值

#6


guid没有任何意义,仅仅作为标识,也不应该赋予它任何含义,
如果输出时不在意顺序,那么就按guid排序好了,
如果需要按照录入先后顺序,那么需要多一个CreateTime或者自增Id作为排序的种子

#7


引用 3 楼 fredrickhu 的回复:
SQL code
取n到m行

1. 
select top m * from tablename where id not in (select top n id from tablename order by id asc/*|desc*/) 

2. 
select top m * into 临时表(或表变量) from tablename order by columnnam……

谢谢详解~

#1


你的问题不在于如何取, 而在于你怎么理解什么是 top  11-20

#2


select top 10 id from tb where id not in(select top 10 id from tb)

#3


取n到m行

1. 
select top m * from tablename where id not in (select top n id from tablename order by id asc/*|desc*/) 

2. 
select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入到临时表 
set rowcount n   --只取n条结果
select * from 表变量 order by columnname desc 

3. 
select top n * from  
(select top m * from tablename order by columnname) a 
order by columnname desc 


4.如果tablename里没有其他identity列,那么: 
先生成一个序列,存储在一临时表中.
select identity(int) id0,* into #temp from tablename 

取n到m条的语句为: 
select * from #temp where id0 > =n and id0  <= m 

如果你在执行select identity(int) id0,* into #temp from tablename这条语句的时候报错,那是因为你的DB中间的select into/bulkcopy属性没有打开要先执行: 
exec sp_dboption 你的DB名字,'select into/bulkcopy',true 


5.如果表里有identity属性,那么简单: 
select * from tablename where identity_col between n and m  

6.SQL2005开始.可以使用row_number() over()生成行号
;with cte as
(
 select id0=row_number() over(order by id),* from tablename
)
select * from cte where id0 between n to m

#4


引用 1 楼 fcuandy 的回复:
你的问题不在于如何取, 而在于你怎么理解什么是 top 11-20

我的这里是想取出企业的名字,然后按照企业下的子公司数量倒序排列取出其中的11-20条~不知道这个能不能用第二种方法来弄~求解答~

#5


建立表变量create @t table(id int identity(1,1),b int)
你的数据插入表变量
从表变量中按照id取出你需要的值

#6


guid没有任何意义,仅仅作为标识,也不应该赋予它任何含义,
如果输出时不在意顺序,那么就按guid排序好了,
如果需要按照录入先后顺序,那么需要多一个CreateTime或者自增Id作为排序的种子

#7


引用 3 楼 fredrickhu 的回复:
SQL code
取n到m行

1. 
select top m * from tablename where id not in (select top n id from tablename order by id asc/*|desc*/) 

2. 
select top m * into 临时表(或表变量) from tablename order by columnnam……

谢谢详解~