您好我在填写SQLite中的交叉表时遇到了一些问题。我只能创建标题但不能填写其中的值。我一直在寻找方法但到目前为止找不到任何东西。任何帮助是极大的赞赏。感谢。
数据集:https://drive.google.com/file/d/1CPW7J4V0RVrmwfR4Opnz-z7BUpdOTJYR/view?usp=sharing
CREATE VIEW avg_salary AS
SELECT team, season, avg(salary) as avg_salary
FROM pergame
GROUP BY team, season;
CREATE VIEW crstab_avg_salary AS
SELECT team AS 'team',
AVG(CASE WHEN season = 2007-08 THEN avg_salary END) AS '2007-08',
AVG(CASE WHEN season = 2008-09 THEN avg_salary END) AS '2008-09',
AVG(CASE WHEN season = 2009-10 THEN avg_salary END) AS '2009-10',
AVG(CASE WHEN season = 2010-11 THEN avg_salary END) AS '2010-11',
AVG(CASE WHEN season = 2011-12 THEN avg_salary END) AS '2011-12',
AVG(CASE WHEN season = 2012-13 THEN avg_salary END) AS '2012-13',
AVG(CASE WHEN season = 2013-14 THEN avg_salary END) AS '2013-14',
AVG(CASE WHEN season = 2014-15 THEN avg_salary END) AS '2014-15',
AVG(CASE WHEN season = 2015-16 THEN avg_salary END) AS '2015-16',
AVG(CASE WHEN season = 2016-17 THEN avg_salary END) AS '2016-17'
FROM avg_salary
GROUP BY team;
.mode csv
.output q4c1.csv
SELECT * FROM crstab_avg_salary;
.output stdout
答案 0 :(得分:2)
试试这个:
SELECT team AS 'team',
AVG(CASE WHEN season = '2007-08' THEN avg_salary END) AS '2007-08',
AVG(CASE WHEN season = '2008-09' THEN avg_salary END) AS '2008-09',
AVG(CASE WHEN season = '2009-10' THEN avg_salary END) AS '2009-10',
AVG(CASE WHEN season = '2010-11' THEN avg_salary END) AS '2010-11',
AVG(CASE WHEN season = '2011-12' THEN avg_salary END) AS '2011-12',
AVG(CASE WHEN season = '2012-13' THEN avg_salary END) AS '2012-13',
AVG(CASE WHEN season = '2013-14' THEN avg_salary END) AS '2013-14',
AVG(CASE WHEN season = '2014-15' THEN avg_salary END) AS '2014-15',
AVG(CASE WHEN season = '2015-16' THEN avg_salary END) AS '2015-16',
AVG(CASE WHEN season = '2016-17' THEN avg_salary END) AS '2016-17'
FROM avg_salary
GROUP BY team;
改变就是我在WHEN
表达式的CASE
子句中引用了季节。例如。 2007-08(阅读2007年减去08)给出1999年,您的数据集中没有数据。