将结果集均分分组,是否有光标?

发布于 2024-08-23 03:04:32 字数 432 浏览 5 评论 0原文

我正在建立一个比赛管理系统,包括车手和预赛。

比方说,我需要将 13 名驾驶员分成几组,每组最多 6 人。这是不可能的,因为结果将是 2.2 组,这是不可能的,需要 3 组。允许少于 6 人的小组,因此我决定将 13 除以 3 来完成以下划分:

热赛 1:4 名车手
预赛 2:4 名车手
预赛3:5名车手(剩下的车手,但不超过6名)

我已经设法划分行并四舍五入结果等,知道需要3组,并且每组允许不少于4名车手。棘手的部分是如何循环遍历这个并在最后一个循环中添加其余的(5)...我正在考虑“SELECT TOP 4...”对于前两个,以及“SELECT TOP 100%...” “对于剩下的五名车手。

我了解游标,但我不是专家,我知道如何创建和执行动态 sql 查询。

如何通过在 SQL Server 2005 中使用游标和/或计数器来完成此操作?

I'm building a race administration system, with drivers and race heats.

I need to divide, lets say, 13 drivers into groups of maximum 6 per group. It's not possible since the result will be 2.2 groups, wich is impossible, 3 groups is required. Smaller groups than 6 is allowed, so I decide to divide 13 by 3 to accomplish the follwing division:

Heat 1: 4 drivers
Heat 2: 4 drivers
Heat 3: 5 drivers (the remaining drivers, but no more than 6)

I have managed to divide the rows and rounding the results etc. to know that 3 groups is needed and no less than 4 drivers per group is allowed. The tricky part is how to loop through this and add the rest (5) in the last loop... I'm thinking of "SELECT TOP 4..." for the two first, and "SELECT TOP 100%..." for the remaining five drivers.

I know cursors, but i'm not an expert and I know how to create and execute a dynamic sql query.

How can this be done by using cursors and/or counters in SQL Server 2005?

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

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

发布评论

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

评论(2

蹲在坟头点根烟 2024-08-30 03:04:32
SELECT  *,
        NTILE((SELECT CAST(CEILING(COUNT(*) / 6.00) AS INT) FROM drivers)) OVER (ORDER BY id) AS heat
FROM    drivers
SELECT  *,
        NTILE((SELECT CAST(CEILING(COUNT(*) / 6.00) AS INT) FROM drivers)) OVER (ORDER BY id) AS heat
FROM    drivers
゛时过境迁 2024-08-30 03:04:32

编辑:Quassnoi 的 NTILE 版本要好得多。

您应该能够使用 while 循环而不是游标来完成此操作。

假设您创建一个包含 DriverId 和 GroupNumber (HeatNumber) 列的表“Race”。您说您知道如何计算有多少个组以及每个组中要放置多少个驱动程序。所以你可以有这样的循环:

set @group = 1
while @group < @totalGroups
begin
    insert Race (DriverId, GroupNumber)
    select top (@driversPerGroup) d.DriverId, @group
    from Drivers d
    where not exists(select * from Race r where r.DriverId = d.DriverId)
    order by whatever

    set @group = @group + 1
end

--insert last group here
insert Race (DriverId, GroupNumber)
select d.DriverId, @totalGroups
from Drivers d
where not exists(select * from Race r where r.DriverId = d.DriverId)

Edit: Quassnoi's NTILE version is much nicer.

You should be able to do this using a while loop instead of a cursor.

Assume you create a table "Race" with columns DriverId and GroupNumber (HeatNumber). You say you know how to calculate how many groups and how many drivers to put in each group. So you can have a loop like this:

set @group = 1
while @group < @totalGroups
begin
    insert Race (DriverId, GroupNumber)
    select top (@driversPerGroup) d.DriverId, @group
    from Drivers d
    where not exists(select * from Race r where r.DriverId = d.DriverId)
    order by whatever

    set @group = @group + 1
end

--insert last group here
insert Race (DriverId, GroupNumber)
select d.DriverId, @totalGroups
from Drivers d
where not exists(select * from Race r where r.DriverId = d.DriverId)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文