How to sum duplicated values in SQL

173 views Asked by At

The sum of the acquisition price of works of art for each year (for example, if there were two works of art purchased for $1500 and $1000 in 2007, and one work of art purchased for $500 in 2008, then the sums would be $2500 and $500, for 2007 and 2008 respectively).

2

There are 2 answers

0
Alan On BEST ANSWER

Assuming your table contains a field containing the year, and a field containing the price, you would simply use:

SELECT AcquisitionYear, SUM(Price) AS TotalPrice
FROM MyTable
GROUP BY AcquisitionYear

If your table contains a date field, you'd need to extract the year from this field using the YEAR() function:

SELECT YEAR(AcquisitionDate), SUM(Price) AS TotalPrice
FROM MyTable
GROUP BY YEAR(AcquisitionDate)
0
Gabor Magyar On

The keyword you're looking for is GROUP BY.

So your query will look something like

SELECT YEAR(date), SUM(price)
FROM tableName
GROUP BY YEAR(date)