例如,我的mysql表中有3行
company name, tracking type, revenue (columns)
company x, yearly, $100000
company x, monthly, $20000
company x, budget, $30000
我需要将公司名称、年度、每月、预算值显示为一条记录,例如
company x, $100000, $20000, $30000
在这里,我需要将它们与公司名称分组
任何帮助将不胜感激
你可以使用GROUP BY
和CASE
SELECT company name
, SUM( CASE tracking type WHEN yearly THEN revenue ELSE 0 END)AS yearly
, SUM( CASE tracking type WHEN monthly THEN revenue ELSE 0 END)AS monthly
, SUM( CASE tracking type WHEN budget THEN revenue ELSE 0 END)AS budget
GROUP BY company name