English 中文(简体)
RANGE 窗口功能条款,以获得以前的金额
原标题:RANGE clause for window function to get the previous sum

我有以下数据,我正试图从前一年获得利润:

WITH tbl (year, country, product, profit) AS (
   VALUES
     (2000,  Finland ,  Computer   , 1500)
   , (2000,  Finland ,  Phone      ,  100)
   , (2001,  Finland ,  Phone      ,   10)
   , (2000,  India   ,  Calculator ,   75)
   , (2000,  India   ,  Calculator ,   75)
   , (2000,  India   ,  Computer   , 1200)
   )
select country, year, profit
     , lag(profit) over (partition by country order by year)
from tbl;

┌─────────┬──────┬────────┬──────────────────────────┐
│ country ┆ year ┆ profit ┆ sum_profit_previous_year │
╞═════════╪══════╪════════╪══════════════════════════╡
│ India   ┆ 2000 ┆     75 ┆                          │
│ India   ┆ 2000 ┆     75 ┆                       75 │
│ India   ┆ 2000 ┆   1200 ┆                       75 │
│ Finland ┆ 2000 ┆   1500 ┆                          │
│ Finland ┆ 2000 ┆    100 ┆                     1500 │
│ Finland ┆ 2001 ┆     10 ┆                      100 │
└─────────┴──────┴────────┴──────────────────────────┘

However, this just seems to get the previous row, rather than what I want, which is to get the LAG of the profit value for the previous year for that country. The expected result should be:

┌─────────┬──────┬────────┬──────────────────────────┐
│ country ┆ year ┆ profit ┆ sum_profit_previous_year |
╞═════════╪══════╪════════╪══════════════════════════╡
│ India   ┆ 2000 ┆     75 ┆                          │
│ India   ┆ 2000 ┆     75 ┆                          │
│ India   ┆ 2000 ┆   1200 ┆                          │
│ Finland ┆ 2000 ┆   1500 ┆                          │
│ Finland ┆ 2000 ┆    100 ┆                          │
│ Finland ┆ 2001 ┆     10 ┆                   1600   │
└─────────┴──────┴────────┴──────────────────────────┘

由于芬兰-2001年是同一国家前一年唯一有记录的记录。 正确的<代码>RANGE 这里有这样的条款? (为测试目的,对大公国或邮政单位都处以罚款)。

问题回答

如果您确实想到所有un-aggregatedrows,并增加aggregated 上一年利润......

这里的是一种简单的窗口功能和习惯窗口框架:

SELECT country, year, profit
     , sum(profit) OVER (PARTITION BY country ORDER BY year
                         RANGE BETWEEN 1 PRECEDING AND 1 PRECEDING) AS sum_profit_previous_year
FROM   tbl
ORDER  BY country DESC, year;  -- optional?

rel=“nofollow noreferer”>fiddle

手册详情见





相关问题
摘录数据

我如何将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 * ...

热门标签