按表中分组记录运行总计
我有一个像这样的表(Oracle,10)
Account Bookdate Amount
1 20080101 100
1 20080102 101
2 20080102 200
1 20080103 -200
...
我需要的是按帐户顺序按帐户 asc 和 Bookdate asc 分组的新表,并带有运行总计字段,如下所示:
Account Bookdate Amount Running_total
1 20080101 100 100
1 20080102 101 201
1 20080103 -200 1
2 20080102 200 200
...
有没有一种简单的方法可以做到这一点?
提前致谢。
I have a table like this (Oracle, 10)
Account Bookdate Amount
1 20080101 100
1 20080102 101
2 20080102 200
1 20080103 -200
...
What I need is new table grouped by Account order by Account asc and Bookdate asc with a running total field, like this:
Account Bookdate Amount Running_total
1 20080101 100 100
1 20080102 101 201
1 20080103 -200 1
2 20080102 200 200
...
Is there a simple way to do it?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您真的需要额外的桌子吗?
您可以通过一个简单的查询获取所需的数据,如果您希望它看起来像表格,您显然可以将其创建为视图。
这将为您提供您正在寻找的数据:
这将创建一个视图来向您显示数据,就像它是一个表一样:
如果您确实需要该表,您是否意味着您需要不断更新它? 或者只是一次? 显然,如果这是一次性的,您可以使用上面的查询“创建表作为选择”。
我使用的测试数据是:
编辑:
忘记添加; 您指定您希望对表进行排序 - 这实际上没有意义,并且让我认为您确实想要查询/视图 - 排序是您执行的查询的结果,而不是固有的东西表(忽略索引组织表等)。
Do you really need the extra table?
You can get that data you need with a simple query, which you can obviously create as a view if you want it to appear like a table.
This will get you the data you are looking for:
This will create a view to show you the data as if it were a table:
If you really need the table, do you mean that you need it constantly updated? or just a one off? Obviously if it's a one off you can just "create table as select" using the above query.
Test data I used is:
edit:
forgot to add; you specified that you wanted the table to be ordered - this doesn't really make sense, and makes me think that you really mean that you wanted the query/view - ordering is a result of the query you execute, not something that's inherant in the table (ignoring Index Organised Tables and the like).
使用分析,就像你的上一个问题:
输出:
Use analytics, just like in your last question:
output:
我将从这个非常重要的警告开始:不要创建一个表来保存这些数据。 当你这样做时,你会发现你需要维护它,这将成为一个永无休止的头痛。 如果您想这样做,请编写一个视图来返回额外的列。 如果您正在使用数据仓库,那么也许您会做类似的事情,但即使如此,也会在视图方面犯错误,除非您根本无法通过索引获得所需的性能,体面的硬件等。
这是一个查询,它将按照您需要的方式返回行。
另一种可能的解决方案是:
测试它们的性能,看看哪个更适合您。 另外,除了您提供的示例之外,我还没有对它们进行彻底的测试,因此请务必测试一些边缘情况。
I'll start with this very important caveate: do NOT create a table to hold this data. When you do you will find that you need to maintain it which will become a never ending headache. Write a view to return the extra column if you want to do that. If you're working with a data warehouse then maybe you would do something like this, but even then err on the side of a view unless you simply can't get the performance that you need with indexes,decent hardware, etc.
Here's a query that will return the rows the way that you need them.
Another possible solution is:
Test them both for performance and see which works better for you. Also, I haven't thoroughly tested them beyond the example which you gave, so be sure to test some edge cases.