如何在没有临时表的 SQL 查询中为组添加序列号

发布于 2024-10-21 00:53:15 字数 537 浏览 2 评论 0原文

我在 SQL 2008 中创建了一个复杂的搜索查询,它返回按组排序的数据,并且该查询本身具有分页和排序功能,但它不是根据分页选项返回一组记录,而是需要返回一个设置组数(因此记录数会有所不同)。

我目前正在通过使用临时表来执行此操作(第一个临时表创建将在搜索中选择的组列表,然后对它们进行编号...第二个查询将此表连接到实际的组搜索...因此,它最终会运行搜索查询两次)。

我正在寻找一种更有效的方法,使用 SQL 2008 中的一些新函数(不需要使用临时表)来完成此操作。

如果我能以这样的格式获取数据,我就会...

Record  Group     GroupSequence
-------|---------|--------------
1       Chickens  1
2       Chickens  1
3       Cows      2
4       Horses    3
5       Horses    3
6       Horses    3

关于如何在 SQL 2008 中使用单个查询来完成此操作而不使用临时表的任何想法?

I've created a complex search query in SQL 2008 that returns data sorted by groups, and the query itself has paging and sorting functions in it, but rather than returning a set number of records based on the paging options, it needs to return a set number of groups (so the number of records will vary).

I'm currently doing this through the use of Temp Tables (the first temp table creates a list of the Groups that will be selected as part of the search, and then numbers them... and the second query joins this table to the actual search... so, it ends up running the search query twice).

What I'm looking for is a more efficient way to do this using some of the new functions in SQL 2008 (which wouldn't require the use of temp tables).

If I can get the data in a format like this, I'd be set...

Record  Group     GroupSequence
-------|---------|--------------
1       Chickens  1
2       Chickens  1
3       Cows      2
4       Horses    3
5       Horses    3
6       Horses    3

Any ideas on how to accomplish this with a single query in SQL 2008, without using temp tables?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静水深流 2024-10-28 00:53:15

示例数据

create table sometable([group] varchar(10), id int, somedata int)
insert sometable select 'Horses', 9, 11
insert sometable select 'chickens', 19, 121
insert sometable select 'Horses', 29, 123
insert sometable select 'chickens', 49, 124
insert sometable select 'Cows', 98, 1
insert sometable select 'Horses', 99, 2

查询

select
    Record = ROW_NUMBER() over (order by [Group], id),
    [Group],
    GroupSequence = DENSE_RANK() over (order by [Group])
from sometable

输出

Record               Group      GroupSequence
-------------------- ---------- --------------------
1                    chickens   1
2                    chickens   1
3                    Cows       2
4                    Horses     3
5                    Horses     3
6                    Horses     3

Sample data

create table sometable([group] varchar(10), id int, somedata int)
insert sometable select 'Horses', 9, 11
insert sometable select 'chickens', 19, 121
insert sometable select 'Horses', 29, 123
insert sometable select 'chickens', 49, 124
insert sometable select 'Cows', 98, 1
insert sometable select 'Horses', 99, 2

Query

select
    Record = ROW_NUMBER() over (order by [Group], id),
    [Group],
    GroupSequence = DENSE_RANK() over (order by [Group])
from sometable

Output

Record               Group      GroupSequence
-------------------- ---------- --------------------
1                    chickens   1
2                    chickens   1
3                    Cows       2
4                    Horses     3
5                    Horses     3
6                    Horses     3
碍人泪离人颜 2024-10-28 00:53:15

如果没有有关您拥有的表的更多详细信息,我建议您查看 CTE 查询和 row_number 函数...大致如下:

;with groups as (
  select top 10 name, row_number() over(order by name) 'sequence'
  from table1
  group by name
  order by name
)
select row_number() over(order by g.name) 'Record',
  g.name 'GroupName',
  g.sequence 'GroupSequence'
from groups

Without more details about the tables you have, I'd say look into CTE queries and the row_number function... something along the lines of:

;with groups as (
  select top 10 name, row_number() over(order by name) 'sequence'
  from table1
  group by name
  order by name
)
select row_number() over(order by g.name) 'Record',
  g.name 'GroupName',
  g.sequence 'GroupSequence'
from groups
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文