Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 从Select top获取数据_Sql Server_Sql Server 2008_Select_Sql Order By - Fatal编程技术网

Sql server 从Select top获取数据

Sql server 从Select top获取数据,sql-server,sql-server-2008,select,sql-order-by,Sql Server,Sql Server 2008,Select,Sql Order By,我之前的问题已经有人回答了 我想从基于字母和编号格式的数据库中获取select top n数据。输出必须先按字母排序,然后按数字排序 当我尝试获取所有数据select*时,我得到了正确的输出: select nocust, share from TB_STOCK where share = ’BBCA’ and concat(share, nocust) < ‘ZZZZZZZZ’ order by case when nocust like ‘[a-z]%’ then 0

我之前的问题已经有人回答了

我想从基于字母和编号格式的数据库中获取select top n数据。输出必须先按字母排序,然后按数字排序

当我尝试获取所有数据select*时,我得到了正确的输出:

select nocust, share 
from TB_STOCK
where share = ’BBCA’ 
  and concat(share, nocust) < ‘ZZZZZZZZ’
order by 
    case when nocust like ‘[a-z]%’ then 0 else 1 end, nocust


nocust | share
-------+--------
a522   | BBCA
b454   | BBCA
k007   | BBCA
p430   | BBCA
q797   | BBCA
s441   | BBCA
s892   | BBCA
u648   | BBCA
v107   | BBCA
4211   | BBCA
6469   | BBCA
6751   | BBCA
问题是,当我试图根据上一次的排名前五的数据获取下一个排名前五的数据时 concatshare,nocust<'ZZZQ797' 它返回错误的预期数据:

select top 5 nocust, share 
from TB_STOCK
where share = ’BBCA’ 
and concat(share, nocust) < ‘ZZZZq797’
order by 
case when nocust like ‘[a-z]%’ then 0 else 1 end, nocust

nocust | share
-------+--------
a522   | BBCA
b454   | BBCA
k007   | BBCA
p430   | BBCA
q797   | BBCA

我想错误在concat和order by之间,有人能告诉我如何获得正确的前5名吗。

我不确定是否有内置函数来获取行范围,但您可以始终使用行编号:

选择nocust,share 从…起 选择nocust,share, 第二排 按大小写排序,如果没有类似“[a-z]%”的命令,则0其他1结束,没有命令 AS RowNum-根据“排序依据”分配行数` 从TB_库存 其中share='BBCA' 和concatshare,nocust<'ZZZZZZZZ' src 其中RowNum介于和之间-获取指定的行范围 订购人 当nocust与“[a-z]%”相似时,则0或1结束,nocust-不确定是否需要
这将根据您的ORDER BY为每一行分配行号,然后仅返回您在WHERE子句中指定的行范围。

我不确定是否有内置函数来获取行范围,但您始终可以使用行号:

选择nocust,share 从…起 选择nocust,share, 第二排 按大小写排序,如果没有类似“[a-z]%”的命令,则0其他1结束,没有命令 AS RowNum-根据“排序依据”分配行数` 从TB_库存 其中share='BBCA' 和concatshare,nocust<'ZZZZZZZZ' src 其中RowNum介于和之间-获取指定的行范围 订购人 当nocust与“[a-z]%”相似时,则0或1结束,nocust-不确定是否需要
这将根据您的ORDER BY为每一行分配行号,然后仅返回您在WHERE子句中指定的行范围。

您应该显示完整的查询

同样,在你的询问中,我和concatshare对这件事也不清楚,不清楚。 我不知道您拥有什么类型的数据,它将返回什么,或者您的预期输出是什么

我想我会在这种情况下创建索引视图

CREATE VIEW dbo.vStockView
WITH SCHEMABINDING
AS
    select  nocust, [share] ,
 0  SortCol
from TB_STOCK
where 
--  share = ’BBCA’ 
--  and concat(share, nocust) < ‘ZZZZZZZZ’ 
 -- and 
  isnumeric(nocust)=0

  union all

  select  nocust, [share] ,
 1  SortCol
from TB_STOCK
where 
  isnumeric(nocust)=1

GO
现在您创建了这样的新进程

Create Proc spGETStock
@share varchar(30)
@PageNumber int,
@RowspPage int=30
as

BEGIN
set nocount on

select nocust,[share] 
--,count(*)over() as TotalRecords
from vStockView
where share = @share
ORDER BY SortCol
OFFSET ((@PageNumber - 1) * @RowspPage) ROWS
FETCH NEXT @RowspPage ROWS ONLY;

End
也可以在SQLServer2008及以下版本中使用行号实现分页


我确信它将显著提高性能。

您应该显示完整的查询

同样,在你的询问中,我和concatshare对这件事也不清楚,不清楚。 我不知道您拥有什么类型的数据,它将返回什么,或者您的预期输出是什么

我想我会在这种情况下创建索引视图

CREATE VIEW dbo.vStockView
WITH SCHEMABINDING
AS
    select  nocust, [share] ,
 0  SortCol
from TB_STOCK
where 
--  share = ’BBCA’ 
--  and concat(share, nocust) < ‘ZZZZZZZZ’ 
 -- and 
  isnumeric(nocust)=0

  union all

  select  nocust, [share] ,
 1  SortCol
from TB_STOCK
where 
  isnumeric(nocust)=1

GO
现在您创建了这样的新进程

Create Proc spGETStock
@share varchar(30)
@PageNumber int,
@RowspPage int=30
as

BEGIN
set nocount on

select nocust,[share] 
--,count(*)over() as TotalRecords
from vStockView
where share = @share
ORDER BY SortCol
OFFSET ((@PageNumber - 1) * @RowspPage) ROWS
FETCH NEXT @RowspPage ROWS ONLY;

End
也可以在SQLServer2008及以下版本中使用行号实现分页


我相信这将显著提高性能。

除此之外,我注意到您尚未接受任何问题的答案。请阅读并考虑接受你现有问题的答案。作为旁白,我注意到你没有接受任何问题的答案。请阅读并考虑接受你现有问题的答案。
Create Proc spGETStock
@share varchar(30)
@PageNumber int,
@RowspPage int=30
as

BEGIN
set nocount on

select nocust,[share] 
--,count(*)over() as TotalRecords
from vStockView
where share = @share
ORDER BY SortCol
OFFSET ((@PageNumber - 1) * @RowspPage) ROWS
FETCH NEXT @RowspPage ROWS ONLY;

End