![](/img/trans.png)
[英]How do I insert into table primary keys from two other tables making every combination?
[英]How to insert data from other tables when the destination table has primary keys?
with product as
( select 1 as productid ,
100.0 as price),
years(year) AS
(
SELECT 2011 UNION ALL SELECT 2012 UNION ALL
SELECT 2013 UNION ALL SELECT 2014 UNION ALL
SELECT 2015 UNION ALL SELECT 2016
)
select
p.productid,
y.year,
power(1.02 , row_number () over (partition by p.productid order by y.year)) * p.price currYearPrice,
power(1.02 , row_number () over (partition by p.productid order by y.year)-1) * p.price lastYearPrice
from product p
cross join years y
结果是:
pID Year CurrYPr LasYPr
1 2011 102.000 100.000
1 2012 104.000 102.000
1 2013 106.000 104.000
1 2014 108.000 106.000
1 2015 110.000 108.000
1 2016 113.000 110.000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.