尝试编写一个返回分页/排序结果的查询,该结果也在桶中进行分组,如下所示:
0-9999,10,000-99,999,10,000-999,999,1,000,000-49,999,999,50,000,000 +
每个人都应该有一个startIndex,当他们点击它时会跳转到哪里
以下是加载此数据的示例查询。用户可以在页面底部看到这些组,但它们取决于他们正在排序的列。单击其中一个组然后将用户跳转到该页面和索引。就我而言,还有更多列,但我认为分组归结为字符串,数字或日期时间。对于字符串,UI希望字母表中的每个字母都是一个组,但只有那些有数据的字母。对于其他一切,应该有大约5或6个桶。
有谁知道怎么做? 甚至这叫什么?在网络上找不到任何东西,但我不确定该找什么。
declare @startIndex int
declare @pageSize int
declare @Names table
(
FirstName varchar(100),
LastName varchar(100),
Age int,
NetWorth money
)
select @startIndex = 1
select @pageSize = 10
insert into @Names (FirstName, LastName, Age, NetWorth) values ('John', 'Smith', 24, 304050)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('John', 'Williams', 31, 430901)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('Adam', 'Wilson', 29, 121000)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('Jen', 'Phillips', 75, 1450000)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('Tommy', 'John', 33, 99000)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('Alex', 'Smith', 48, 12800000)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('Dianne', 'Lane', 60, 94000000)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('Charles', 'Barkley', 46, 21500000)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('Will', 'Allen', 21, 4000)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('Michael', 'Jordan', 50, 94500000)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('Jenny', 'Block', 43, 509000)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('Oprah', 'Winfrey', 61, 55000000)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('Charles', 'Smith',50, 3400000)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('LeBron', 'James', 29, 74678000)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('Chris', 'Paul', 30, 19400000)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('Kobe', 'Bryant', 38, 124600000)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('Antonio', 'Gates', 36, 17040190)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('Tony', 'Romo', 35, 28403400)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('Nancy', 'Cammo', 44, 167880)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('Bill', 'Brown', 45, 98000)
insert into @Names (FirstName, LastName, Age, NetWorth) values ('Paul', 'Jones', 26, 987900);
WITH SearchResults AS
(
SELECT
'RowNum' = ROW_NUMBER() over(ORDER BY NetWorth asc)
,Age
,NetWorth
,FirstName
,LastName
FROM @Names
group by Age, NetWorth, FirstName, LastName
),
counts as (
select 'TotalRows' = count(*) from @Names
),
maxvalue as (
select 'Val' = max(NetWorth) from @Names
)
SELECT sr.RowNum
,sr.FirstName
,sr.LastName
,sr.Age
,sr.NetWorth
,'TotalRows' = c.TotalRows
,'MaxValue' = mv.Val
FROM SearchResults sr
cross apply counts c
cross apply maxvalue mv
WHERE sr.RowNum BETWEEN @startIndex AND @pageSize - @startIndex + 1
/*
select NetWorth,
count(NetWorth)
from @Names
group by NetWorth
order by NetWorth
*/
任何帮助都将不胜感激。
答案 0 :(得分:0)
这是一次尝试,我没有sql-server(我假设您正在使用),因此您可能需要轻微调整查询:
select * from (
select ROW_NUMBER() over(ORDER BY NetWorth asc) as rn
, Age, NetWorth, FirstName, LastName
, count(*) over () as totalrows
, max(networth) over () as max_value
from names
) as t
where rn between 1 and 10;