By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE dt
([id] varchar(36), [pracownik] varchar(4), [project] varchar(36), [year] int, [month] int, [amount] int)
;
INSERT INTO dt
([id], [pracownik], [project], [year], [month], [amount])
VALUES
('c38af663-f579-4984-9bf5-8408722c3ba9', NULL, '0c16e38c-57e0-474b-a7d3-ba870124a883', 2018, 12, 80),
('c38af663-f579-4984-9bf5-8408722c3ba9', NULL, '0c16e38c-57e0-474b-a7d3-ba870124a883', 2019, 1, 60),
('c38af663-f579-4984-9bf5-8408722c3ba9', NULL, '0c16e38c-57e0-474b-a7d3-ba870124a883', 2019, 2, 65)
;
3 rows affected
select * from (
select
*,
row_number() over (partition by id,pracownik,project order by year,month) r,
year(dateadd(m,1,datefromparts(year,month,1))) year1,
month(dateadd(m,1,datefromparts(year,month,1))) month1,
lead(amount,1,0) over (partition by id,pracownik,project order by year,month) amount1,
year(dateadd(m,2,datefromparts(year,month,1))) year2,
month(dateadd(m,2,datefromparts(year,month,1))) month2,
lead(amount,2,0) over (partition by id,pracownik,project order by year,month) amount2
from dt) x where r=1
id | pracownik | project | year | month | amount | r | year1 | month1 | amount1 | year2 | month2 | amount2 |
---|---|---|---|---|---|---|---|---|---|---|---|---|
c38af663-f579-4984-9bf5-8408722c3ba9 | null | 0c16e38c-57e0-474b-a7d3-ba870124a883 | 2018 | 12 | 80 | 1 | 2019 | 1 | 60 | 2019 | 2 | 65 |