My need is to retrieve sell data from and pivot the last three years and sum the quantities. See example below. I wonder how to do it. I read that SQL pivot is the way to go, but I can't figure out how to deal with pivoting "automatically" N number of years in the past.
INPUT
+----------+----------+------+
| ItemCode | Quantity | Date |
+----------+----------+------+
| A | 100 | 2017 |
+----------+----------+------+
| B | 200 | 2017 |
+----------+----------+------+
| B | 200 | 2017 |
+----------+----------+------+
| A | 50 | 2018 |
+----------+----------+------+
| A | 170 | 2018 |
+----------+----------+------+
| A | 75 | 2019 |
+----------+----------+------+
| B | 10 | 2019 |
+----------+----------+------+
OUTPUT
+---+------+------+------+
| | 2017 | 2018 | 2019 |
+---+------+------+------+
| A | 100 | 220 | 75 |
+---+------+------+------+
| B | 400 | - | 10 |
+---+------+------+------+