to be more clear I have these kind of data.
Query 1) Data from 2016
Item Price Quantity
Shoe 20 10
Shoe 30 15
Cups 10 30
Towels 30 20
Towels 25 20
Towels 20 20
Query 2) Data from 2017
Item Price Quantity
Shoe 40 30
Shoe 50 20
Towels 30 30
Towels 20 30
Query 3) Data from 2018
Item Price Quantity
Shoe 20 10
Cups 10 30
Towels 30 20
Towels 25 20
Towels 20 20
Query 1) Data from 2019
Item Price Quantity
Shoe 20 10
Shoe 30 15
Cups 10 30
Towels 30 20
Towels 25 20
Towels 20 20
I would like to have a result like this:
Item Price2016 Quantity2016 Price2017 Quantity2017 Price2018 Quantity2018 Price2019 Quantity2019
Shoe 20 10 40 30 20 10 20 10
Shoe 30 15 50 20 30 15
I tried using Joins
, Unions
, even creating Temp Tables
or a cursor
for example, inner Join
produce this result:
Item Price2016 Quantity 2016 Price2017 Quantity 2017 ...
Shoe 20 10 20 10
Shoe 20 10 20 10
Shoe 20 10 20 10
Shoe 20 10 20 10
Shoe 20 10 20 10
Shoe 20 10 20 10
Shoe 20 10 20 10
Shoe 20 10 20 10
Please do take note that the data in this example is innacurate but the result is similar to this.
Any Idea how can I obtain my preferred result using SQL
EDIT: The query that I get the data from is this
select Item, Price, sum(quantity) as quantity from Sales where year(itemsold) = 2016 group by Item, price
I just change the year to get the other data.