How do I find the sum of two categories for each year in this PostgreSQL dataset?

杀马特。学长 韩版系。学妹 提交于 2019-12-11 04:23:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!