MS SQL 2000 - 如何有效地遍历一组先前的记录并分组处理它们。大桌子

发布于 2024-10-18 13:45:02 字数 1148 浏览 0 评论 0原文

我想咨询一件事。我在数据库中有表。它有 2 列,如下所示:

Name...bilance

Jane...+3
简...-5
简...0
简...-8
简...-2
保罗...-1
保罗...2
保罗....9
保罗...1
...

我必须遍历这个表,如果我发现具有不同“名称”的记录(与前一行相比),我会处理具有前一个“名称”的所有行。 (如果我踩到第一 Paul 行,我会处理所有 Jane 行)

处理过程如下:

现在我只处理 Jane 记录并逐条浏览它们。在每条记录上,我都会停下来,将其与 Jane 之前的所有行一一进行比较。

任务是总结“bilance”列(在实际人的范围内),如果它们有不同的符号

摘要:

我以 3 个级别并行循环遍历此表(嵌套循环)
第一级 = 搜索“名称”列的更改
第二级 = 如果发现更改,则获取具有先前“名称”的所有行并遍历它们
第三级 = 在每行上停止并遍历当前“名称”的所有先前行

只能使用 CURSOR 和 FETCHING 来解决这个问题,还是有一些更平滑的解决方案?

我的真实表有 30 000 行和 1500 人,如果我在 PHP 中执行逻辑,则需要很长时间,而且会超时。所以我想将其重写为MS SQL 2000(不允许使用其他数据库)。游标是快速解决方案还是使用其他方法更好?

谢谢您的意见。

更新:

关于我的“总结”有很多问题。问题比我解释的要困难一点。我简化它只是为了描述我的算法。

我的表格的每一行都包含更多的列。最重要的是月份。这就是为什么每个人都有更多的行。每个都针对不同的月份。
“Bilances”是工人的“加班”和“拖欠时间”。我需要总结+和-平衡,以使用前几个月的值来抵消它们。我想要有尽可能多的零。所有表格必须保持原样,只是余额必须更改为零。

示例:

行 (Jane -5) 将与行 (Jane +3) 进行汇总。我将得到 0,而不是 3;我将得到 -2,而不是 -5。因为我用这个-5来减少+3。
下一行 (Jane 0) 不会受到影响
下一行 (Jane -8) 无法使用,因为之前的所有余额均为负
ETC。

I'd like to consult one thing. I have table in DB. It has 2 columns and looks like this:

Name...bilance

Jane...+3
Jane...-5
Jane...0
Jane...-8
Jane...-2
Paul...-1
Paul...2
Paul....9
Paul...1
...

I have to walk through this table and if I find record with different "name" (than was on previous row) I process all rows with the previous "name". (If I step on the first Paul row I process all Jane rows)

The processing goes like this:

Now I work only with Jane records and walk through them one by one. On each record I stop and compare it with all previous Jane rows one by one.

The task is to sumarize "bilance" column (in the scope of actual person) if they have different signs

Summary:

I loop through this table in 3 levels paralelly (nested loops)
1st level = search for changes of "name" column
2nd level = if change was found, get all rows with previous "name" and walk through them
3rd level = on each row stop and walk through all previous rows with current "name"

Can this be solved only using CURSOR and FETCHING, or is there some smoother solution?

My real table has 30 000 rows and 1500 people and If I do the logic in PHP, it takes long minutes and than timeouts. So I would like to rewrite it to MS SQL 2000 (no other DB is allowed). Are cursors fast solution or is it better to use something else?

Thank you for your opinions.

UPDATE:

There are lots of questions about my "summarization". Problem is a little bit more difficult than I explained. I simplified it just to describe my algorithm.

Each row of my table contains much more columns. The most important is month. That's why there are more rows for each person. Each is for different month.
"Bilances" are "working overtimes" and "arrear hours" of workers. And I need to sumarize + and - bilances to neutralize them using values from previous months. I want to have as many zeroes as possible. All the table must stay as it is, just bilances must be changed to zeroes.

Example:

Row (Jane -5) will be summarized with row (Jane +3). Instead of 3 I will get 0 and instead of -5 I will get -2. Because I used this -5 to reduce +3.
Next row (Jane 0) won't be affected
Next row (Jane -8) can not be used, because all previous bilances are negative
etc.

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

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

发布评论

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

评论(3

旧情别恋 2024-10-25 13:45:02

您可以使用单个 SQL 语句对每个名称的所有值求和:

select
    name,
    sum(bilance) as bilance_sum
from
    my_table
group by
    name
order by
    name

You can sum all the values per name using a single SQL statement:

select
    name,
    sum(bilance) as bilance_sum
from
    my_table
group by
    name
order by
    name
久夏青 2024-10-25 13:45:02

从表面上看,这听起来应该可以满足您的要求:

select Name, sum(bilance)
from table
group by Name
order by Name

如果没有,您可能需要详细说明名称的排序方式以及“汇总”的含义。

On the face of it, it sounds like this should do what you want:

select Name, sum(bilance)
from table
group by Name
order by Name

If not, you might need to elaborate on how the Names are sorted and what you mean by "summarize".

还在原地等你 2024-10-25 13:45:02

我不确定你的意思是什么......“任务是总结“bilance”列(在实际人的范围内)如果他们有不同的迹象”。

但是,也许可以使用分组查询来获得您需要的大量内容。

select name, case when bilance < 0 then 'negative' when bilance >= 0 then 'positive', count(*)
from table
group by name, bilance

对于 case 语句来说,这可能不是完美的语法,但它应该能让您真正接近。

I'm not sure what you mean by this line... "The task is to sumarize "bilance" column (in the scope of actual person) if they have different signs".

But, it may be possible to use a group by query to get a lot of what you need.

select name, case when bilance < 0 then 'negative' when bilance >= 0 then 'positive', count(*)
from table
group by name, bilance

That might not be perfect syntax for the case statement, but it should get you really close.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文