我有两张桌子sugar_distribution 和farmer_dir。
Sugar_distribution 表包含 season_cd、farmer_id 和 Tons_supplied 列。farmer_dir 包含farmer_name 和farmer_id。我需要的是
Farmer_id Farmer_name Tons@2006 Tons@2012
123 Ramesh G 145.215 548.125
321 Ravish 214.123 783.215
season_cd 是年份字段。我想分析 2006 年和 2012 年提供的各方。我尝试了以下查询,但没有得到我想要的。
SELECT DISTINCT farmer_id,
(SELECT sum(tons)
FROM sugar_distribution
WHERE (farmer_id = A.farmer_id)
AND (season_cd = 2)) AS T2006,
(SELECT sum(tons)
FROM sugar_distribution
WHERE (farmer_id = A.farmer_id)
AND (season_cd = 8)) AS T2012
FROM sugar_distribution A
WHERE A.season_cd = 2 OR A.season_cd = 8 order by to_number(farmer_id)