English 中文(简体)
PostgreSQL - WHERE Clause within overf.
原标题:PostgreSQL - WHERE clause within OVER clause?
  • 时间:2011-06-20 22:31:31
  •  标签:
  • postgresql

我需要使用超出条款的条款。 如何?

SELECT SUM(amount) OVER(WHERE dateval > 12)

或像这样的东西。

页: 1

更多细节

我的表格分为一年、月和数额一栏。

我想选择所有年份、月份和数额,并创建第四个虚拟栏目,其编号为:过去12个月的总和。

例如:

YEAR | MONTH | AMOUNT
2001 | 03 | 10
2001 | 05 | 25
2001 | 07 | 10

应创造:

YEAR | MONTH | AMOUNT | ROLLING 12 MONTHS
2001 | 03 | 10 | 10
2001 | 05 | 25 | 35
2001 | 07 | 10 | 45

最佳回答

鉴于有人对你的三栏成果表示怀疑,以下工作是否为你服务?

SELECT
    SUM(amount) OVER(ORDER BY YEAR ASC, MONTH ASC
                     ROWS BETWEEN 11 PRECEDING AND CURRENT ROW)
...
问题回答
select a,(select sum(a) from foo fa where fa.a > fb.a) from foo fb;

不管怎样使用,其效率都相当低,因为它为每个电梯运行新的分局,但它运作良好。





相关问题
摘录数据

我如何将Excel板的数据输入我的Django应用? I m将PosgreSQL数据库作为数据库。

Postgres dump of only parts of tables for a dev snapshot

On production our database is a few hundred gigabytes in size. For development and testing, we need to create snapshots of this database that are functionally equivalent, but which are only 10 or 20 ...

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example select id, (name + + surname + + age) as info from users this doesn t work, how to do it? I m using postgreSQL.

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

SQL LIKE condition to check for integer?

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A": SELECT * ...

热门标签