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.
You seem to be looking for
GROUP BY
and aggregate functions, not self-joins: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.