我有两个类型(类型,播放器)和播放器(播放器,日期)的表 我必须每年都能找到有多少人参加比赛,哪种类型的球员获胜?
例如表格应该像
year type noofawards
2011 batsmen 3
2011 bowler 5
2010 batsmen 2
我可以获得每年赢得的奖项总数,但不能将它们分类,所以我得到的是
year noofawards
2011 3
select year , count(year) as "Number of awards"
from
(
select to_char(p.date,'YYYY') as year
from player p, type t
where p.playerid = t.playerid
)
group by year
order by year;
我该怎么办?
答案 0 :(得分:1)
您是否尝试过以下操作:
select to_char(p.date,'YYYY') as year
, type
, count(*)
from player p, type t
where p.playerid = t.playerid
group by 1,2;
答案 1 :(得分:1)
只需使用:
GROUP BY year
, type
YEAR()
或类似的名称)来从日期中提取年份。date
不是一个好主意。JOIN
语法而不是WHERE
的隐式联接。类似的东西:
SELECT YEAR(p.date) AS awardYear
, t.type
, COUNT(*) AS "Number of awards"
FROM player p
JOIN type t
ON p.playerid = t.playerid
GROUP BY awardYear
, t.type
ORDER BY awardYear
, t.type ;
答案 2 :(得分:1)
由于这个家庭作业的尖叫,我只会给出提示。
您不需要使用子查询,您需要在查询中的某个位置实际选择“类型”列。
答案 3 :(得分:0)
这应该有效:
SELECT year,
type,
COUNT(*) as number
FROM (
SELECT type,
to_char(player.date,'YYYY') as year
FROM player
NATURAL JOIN type
) AS T
GROUP BY year,
type