2011-06-01 28 views
1

我有點卡住,並希望得到幫助.....我敢肯定,我需要使用子查詢,但是當我嘗試即時獲取錯誤說,返回多個行... 。SQL子查詢疑問

所以這是代碼

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 
+0

你能發佈您試圖使用的子查詢? – 2011-06-01 16:29:04

回答

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; 
+0

另一件事...如果我想只顯示具有更多1釋放的流派,怎麼辦...... – John 2011-06-01 16:30:21

+1

@John看到我的更新,你可以使用'HAVING'子句 – RedFilter 2011-06-01 16:35:37

+0

謝謝....我忘了數數。 ...我一直試着用count(*)> 1 ..... – John 2011-06-01 16:37:16

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