问题
Here's my dataset (in CSV form for easy reading):
item_category,year,price
"Bacon",1890,0.12
"Bacon",1891,0.126
"Bacon",1892,0.129
"Bacon",1893,0.142
"Eggs (dozen)",1890,0.208
"Eggs (dozen)",1891,0.221
"Eggs (dozen)",1892,0.221
"Eggs (dozen)",1893,0.224
I'd like to find the sum for the price of bacon and eggs for each year. So, the result I want would be:
item_category,year,price
"Bacon and eggs",1890,0.328
"Bacon and eggs",1891,0.347
"Bacon and eggs",1892,0.35
"Bacon and eggs",1893,0.366
I'm a novice at SQL, and I've been looking into Self-Join methods, but they don't seem to be exactly what I'm looking for -- I've basically run out of ideas for now.
If it helps, I also know how to use the Sequel gem for Ruby.
回答1:
You seem to be looking for GROUP BY and aggregate functions, not self-joins:
SELECT string_agg(DISTINCT item_category, ' and ' ORDER BY item_category)
AS items
, year
, sum(price) AS price
FROM tbl
GROUP BY year
ORDER BY year
string_agg(DISTINCT item_category, ' and ' ORDER BY item_category)
optionally concatenates distinct names to form the new label automatically. May work for you. Else replace it with your static label.
回答2:
select "Bacon and eggs", year, sum(price)
from table_name
group by "Bacon and eggs", year
order by year;
来源:https://stackoverflow.com/questions/12645535/how-do-i-find-the-sum-of-two-categories-for-each-year-in-this-postgresql-dataset