これが私が持っているテーブルです
Company | Year | Amount
------------------------
CompanyA | 2008 | 5
CompanyA | 2008 | 4
CompanyB | 2008 | 4
CompanyB | 2009 | 1
CompanyC | 2009 | 4
CompanyC | 2010 | 2
疑似コード
SELECT company,
CASE WHEN year = 2008 THEN select max(amount) for each company where the year is = 2008
CASE WHEN year = 2009 THEN select max(amount) for each company where the year is = 2009
GROUP BY company
CASEを機能させることができましたが、すべての年のMAX(金額)を選択し、会社ごとにグループ化していますが、年ごとの最大値が必要です。
CASE 式の条件を WHERE 句またはネストされた CASE で修飾する必要があると思いますが、機能させることができません。
実際のコード
select
record_id AS record_id,
invoice_date AS invoice_date,
company_id AS company_id,
company_name AS company_name,
fiscal_year AS fiscal_year,
(CASE fiscal_year WHEN '2008' THEN MAX(amount) ELSE 0 END) AS `2008`,
(CASE fiscal_year WHEN '2009' THEN MAX(amoutn) ELSE 0 END) AS `2009`,
(CASE fiscal_year WHEN '2010' THEN MAX(amount) ELSE 0 END) AS `2010`,
(CASE fiscal_year WHEN '2011' THEN MAX(amount) ELSE 0 END) AS `2011`,
(CASE fiscal_year WHEN '2012' THEN MAX(amount) ELSE 0 END) AS `2012`,
(CASE fiscal_year WHEN '2013' THEN MAX(amount) ELSE 0 END) AS `2013`
from tbl
group by company_name
order by invoice_date desc, record_id desc;
どんな助けでも大歓迎です!どうも