有人可以向我解释一下本教程中的运行总计和 SQL 自连接吗?
我正在阅读这里的教程: http://www.1keydata.com/ sql/sql-running-totals.html 这一切都是有道理的,直到它突然变得极其可笑的难以置信的复杂,当它涉及到排名,中位数,运行总计等时。有人可以用简单的英语解释一下该查询的结果是如何的吗?运行总计?谢谢!
I was reading over the tutorial here: http://www.1keydata.com/sql/sql-running-totals.html and it all made sense until it suddenly got extremely ridiculously unbelievably complicated when it got to rank, median, running totals, etc. Can somebody explain in plain English how that query results in a running total? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在开始之前,我以前从未见过这种情况,而且它看起来并不是一种非常容易理解的完成运行总计的方法。
好的,这是教程中的查询:
示例输出
该查询的简单部分是显示每个员工的销售数据。我们所做的就是从每个员工中选择
name
和sales
并按销售额(降序)对它们进行排序。这给了我们我们的基本列表。现在对于运行总计,我们需要已经显示的每一行。因此,我们在已经显示的每一行上将表与自身连接起来:
然后我们使用
SUM
聚合函数并进行相应的分组。理解这一点的一个好方法是看看如果不使用组功能会发生什么。 “Sophia”行看起来像这样:注意到我们如何获得 Greg 的销售行了吗?小组将对此进行总结,瞧!
希望有帮助。乔
Before I get started, I've not seen this before and it doesn't look like a terribly comprehensible way to accomplish a running total.
Okay, here's the query from the tutorial:
And the sample output
The simple part of this query is displaying the sales data for each employee. All we're doing is selecting
name
andsales
from each employee and ordering them by the sale amount (descending). This gives us our base list.Now for the running total, we want every row that has already been displayed. So, we join the table against itself, on each row that would already have been displayed:
Then we use the
SUM
aggregate function and group accordingly. A good way to understand this is if you look at what would happen if you didn't use the group function. The 'Sophia' row would look like this:Notice how we got Greg's sales row? The group will sum that up, and voila!
Hope that helps. Joe
我也得到了与上面 Bob 相同的错误输出,其中 Stella & 的运行总数崩溃了。杰夫,他们有相同的销售数字。我正在使用 SQL Server 2014 Management Studio Express。我认为该网站的解决方案实际上并不正确。我根据名称而不是销售额进行连接,并得出了这些结果,从而产生了正确的运行总计:
产量:
如果您对聚合排序感到不舒服,您也可以执行下面的变体。它改变了顺序,但运行总计仍然正确:
产量:
I also get the same incorrect output as Bob above where the running total breaks down at Stella & Jeff, who have the same sales number. I'm using SQL Server 2014 Management Studio Express. I don't think the website's solution is actually correct. I did the join based on name instead of on sales and came up with these, which produce a correct running total:
Yields:
You could also do the variant below if you're uncomfortable sorting on an aggregate. It changes the order, but the running total is still correct:
Yields:
第一个表连接到自身,连接产生 x 行数,其中 x 是总销售额低于其自身的行数,或者行中的名称相同(即我们所在行之前的所有销售额)正在查看,按销售额订购时)。
然后,它对连接左侧的字段进行分组,并对我们连接到的行进行求和,从而得出运行总计。要了解它是如何工作的,您可能希望在没有求和和分组的情况下运行它,以查看返回的原始结果。
The first table joins to itself, the join resulting in x number of rows, where x is the number of rows that have total sales lower than itself, or the name in the row is the same (i.e. all those sales previous to the row we are looking at, when ordered by sales amount).
It then groups on the fields in the left side of the join and sums the rows we join to, thus a running total. To see how it works, you might want to run it without the sum and grouping, to see the raw results returned.
上面的 SQL 在 Sybase (ASE 15) 上给出了不同的结果。我认为原因是“排序依据”直到显示时间才应用。这是 SQL 和结果:
结果:
Bob
The SQL above gives a different result on Sybase (ASE 15). I think the reason is that the 'order by' is not applied until display time. Here is the SQL and the result:
Result:
Bob