我是SQL的新手,想从列表中创建一个名为“ firstprice”和“ lastprice”的列。首先按零件号划分价格,然后按提取的登记年份(零件价格)订购。我的代码似乎忽略了ORDER BY代码,因为我仅获得该商品的First_Value(价格)。 (并且不按年份排序)
SELECT
Prices."Part",
Prices."Price",
Prices."RegDt"
CAST(FIRST_VALUE("Price" IGNORE NULLS)
OVER (PARTITION BY PB."Part" ORDER BY (EXTRACT (YEAR FROM(Prices."RegDt"))))AS INTEGER) AS "FirstPrice",
CAST(LAST_VALUE("Price" IGNORE NULLS)
OVER (PARTITION BY PB."Part" ORDER BY (EXTRACT (YEAR FROM(Prices."RegDt"))))AS INTEGER) AS "FirstPrice",
FROM Prices
预期的输出是该年的第一笔价格,但我只获得该产品的第一笔价格(每年未显示)
答案 0 :(得分:2)
预期产量是当年首次注册价格
因此,您需要在partition
子句中添加年份。我还为last_value
定义了window子句,否则它是无限制的前一行和当前行,我们希望从此处获取下一行的数据:
select part, price, regdt,
first_value(price) over (partition by part, extract (year from(regdt))
order by regdt) fp,
last_value(price) over (partition by part, extract (year from(regdt))
order by regdt
range between current row
and interval '1' year following ) lp
from prices