cancel
Showing results for 
Search instead for 
Did you mean: 

combining pivot and window

Baron
Participant
742

I can't solve this "simple" problem, can maybe someone help?

What I have is:

create or replace table mytable (whichamount int, whichdate date)
insert into mytable values
(100, '2024-01-01'),
(200, '2024-01-01'),
(300, '2024-02-01'),
(400, '2024-03-01'),
(500, '2024-04-01'),
(600, '2024-04-01');
What I want is:
whichdate, whichamount1, whichamount2
'2024-01-01', 100, 200
'2024-02-01', 300, null
'2024-03-01', 400, null
'2024-04-01', 500, 600

Accepted Solutions (1)

Accepted Solutions (1)

VolkerBarth
Contributor

Here's a similar take with PIVOT:

SELECT *
FROM ( SELECT 
        whichdate,
        whichamount,
        ROW_NUMBER() OVER (PARTITION BY whichdate ORDER BY whichamount) AS rn
    FROM mytable
     ) PivotSourceData
   PIVOT ( 
      LIST (whichamount)
      FOR rn IN ( 1 as whichamount1, 2 as whichamount2)
   ) PivotedData
ORDER BY whichdate;

Answers (0)