SQL Sub查询疑问

时间:2011-06-01 16:24:42

标签: sql subquery

我有点卡住了,并且会很感激帮助.....我确定我需要使用子查询,但是当我尝试即可获得错误时说返回多行......

所以这是代码

SELECT to_char(p.releasedate, 'YYYY') AS releaseyear , t.genre
, COUNT(*) AS "Number of awards"
FROM dbf11.film p
JOIN dbf11.genre t
ON p.filmID = t.filmID
GROUP BY p.releasedate, t.genre
ORDER BY p.releasedate, t.genre ;

我想做的是....每年都会显示不行。各种类型的电影

我目前得到的是

2010 Action 1
2010 Comedy 1
2010 Comedy 1
2011 Action 1
2011 Action 1
2011 Action 1

我希望它显示的是

2010 Action 1
2010 Comedy 2
2011 Action 3

2 个答案:

答案 0 :(得分:7)

SELECT to_char(p.releasedate, 'YYYY') AS releaseyear, 
    t.genre.
    COUNT(*) AS "Number of awards"
FROM dbf11.film p
INNER JOIN dbf11.genre t ON p.filmID = t.filmID
GROUP BY to_char(p.releasedate, 'YYYY'), t.genre
HAVING COUNT(*) > 1
ORDER BY to_char(p.releasedate, 'YYYY'), t.genre;

答案 1 :(得分:1)

试试这个

SELECT to_char(p.releasedate, 'YYYY') AS releaseyear , t.genre
, COUNT(*) AS "Number of awards"
FROM dbf11.film p
JOIN dbf11.genre t
ON p.filmID = t.filmID
GROUP BY to_char(p.releasedate, 'YYYY'), t.genre
ORDER BY releaseyear, t.genre