我有一個SQL查詢向我提供數據庫中的雙記錄列表。從SQL子查詢中獲取更多詳細信息
select periodid, itemid from periodscore
group by periodid, itemid
having count(*) > 1
這個按預期工作,但現在我想檢索這些記錄的其他字段(如日期最後更新等)。所以,我想:
select * from periodscore where periodscoreid in
(select periodscoreid from periodscore
group by periodid, itemid
having count(*) > 1)
當然,這並不工作,給我的錯誤:
Column 'periodscore.PeriodScoreID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
我怎樣才能檢索到在此查詢的附加字段?
謝謝!這個查詢也給我216記錄,而不是原來的103 – Sam 2010-09-08 11:41:24
@Sam - 第一個查詢告訴你的組。這會爲您提供屬於這些組的行。當然,所有的組將至少有2行,因爲這是查詢的目的!如果你只想每組返回一行,那麼哪一行? – 2010-09-08 11:48:54
嗯,好點:)謝謝你的答案! – Sam 2010-09-08 11:53:38