'How to re-structure/re-shape a SQL table based on a time window?

I have a table like

ID       CloseDate    ClosePrice    YearOfOrder
9107    2013-02-13 00:00:00 184.00000000    2013
9206    2013-02-13 00:00:00 2231.00000000   2013
9376    2013-02-13 00:00:00 55200.00000000  2013
9404    2013-02-13 00:00:00 1351.00000000   2013
9430    2013-02-13 00:00:00 4085.00000000   2013
9432    2013-02-13 00:00:00 4170.00000000   2013
9433    2013-02-13 00:00:00 6850.00000000   2013
9436    2013-02-13 00:00:00 2029.00000000   2013
9437    2013-02-13 00:00:00 139100.00000000 2013
9449    2013-02-13 00:00:00 705.00000000    2013
9601    2013-02-13 00:00:00 974.00000000    2013
1333    2018-05-15 00:00:00 4080.00000000   2018
1605    2018-07-23 00:00:00 1161.50000000   2018
1605    2018-08-27 00:00:00 1205.50000000   2018
1605    2018-09-24 00:00:00 1391.00000000   2018
1605    2018-10-22 00:00:00 1424.00000000   2018
1407    2018-11-26 00:00:00 1276.00000000   2018
2154    2018-12-31 00:00:00 2765.00000000   2018
TPFI    2019-02-04 00:00:00 587.01000000    2019
2531    2021-05-05 00:00:00 1409.00000000   2021

basically a dataset of 12years for a series of stocks, some of these stocks are always there but some were in data for a short time only. now I need to pick the ClosePrice for all the stocks in a specific year and re-arrange the table like below, because I have to calculate the returns for each stock. let's assume the 2021

CloseDate   9107 9206 9376 9404   
2021-01-06    
2021-01-07    
2021-01-08    
..
..
2021-12-30    

I am new to SQL and stuck and don't know how to do it, please help. I tried below code but no luck

SELECT * FROM PH
PIVOT (SUM([ClosePrice]
FOR YearOfOrder IN ( [2021]))
as PVT

**error Incorrect syntax near the keyword 'FOR'.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source