내 데이터 세트는 (Ruby gem Sequel을 사용하고 있으므로 해시 형식으로); (필자는 루비 보석 속편을 사용하고 있기 때문에 해시에서)다른 열을 PostgreSQL의 같은 행에 넣으려면 어떻게해야합니까?
SELECT
string_agg(DISTINCT item_category, ' + ' ORDER BY item_category) AS items,
year,
sum(avg_yr_value) AS amount
FROM
earnings_and_prices
WHERE
item_category = 'Bacon' OR
item_category = 'Eggs (dozen)'
GROUP BY
year
HAVING
COUNT(DISTINCT item_category) = 2
ORDER BY
year
내 결과 :
{:items=>"Bacon + Eggs (dozen)", :year=>1890, :amount=>0.328}
{:items=>"Bacon + Eggs (dozen)", :year=>1891, :amount=>0.347}
{:items=>"Bacon + Eggs (dozen)", :year=>1892, :amount=>0.35}
{:items=>"Bacon + Eggs (dozen)", :year=>1893, :amount=>0.366}
I
다음{:item_category=>"Bacon", :year=>1890, :avg_yr_value=>0.12}
{:item_category=>"Eggs (dozen)", :year=>1890, :avg_yr_value=>0.208}
{:item_category=>"Bacon", :year=>1891, :avg_yr_value=>0.126}
{:item_category=>"Eggs (dozen)", :year=>1891, :avg_yr_value=>0.221}
{:item_category=>"Bacon", :year=>1892, :avg_yr_value=>0.129}
{:item_category=>"Eggs (dozen)", :year=>1892, :avg_yr_value=>0.221}
{:item_category=>"Bacon", :year=>1893, :avg_yr_value=>0.142}
{:item_category=>"Eggs (dozen)", :year=>1893, :avg_yr_value=>0.224}
{:item_category=>"Bacon", :year=>1894, :avg_yr_value=>0.185}
내 코드입니다 : 해시 이름은 열 이름입니다 "Bacon"에 대한 금액과 "Eggs (dozen)"에 대한 금액을 포함하도록 코드를 편집하고 싶습니다. 예 :
{:items=>"Bacon + Eggs (dozen)", :year=>1890, :amount=>0.328, :eggs_price=>0.208, :bacon_price=>0.12}
어떻게해야합니까?
"10 x Bacon + 20 x Eggs"와 같이 _items_ 출력 열에 각 항목의 개별 금액을 갖는 것을 의미합니까? – ubik
아, 죄송합니다. 나는 내가 원하는 것을 보여 주었다. – user1626730