求ms sql语句,我想查询出新闻表中每个新闻分类的中点击量最高的前两条新闻?
求ms sql语句,我想查询出新闻表中每个新闻分类的中点击量最高的前两条新闻,新闻分类数量是不固定的!假如新闻分类有10个,那么出来的结果是20条!
新闻表中的字段:id title content click type addTime
SELECT * FROM News AS T WHERE ID IN(SELECT TOP 2 ID FROM News WHERE [type]=T.[type] ORDER BY Click DESC);
试试啦,假设News为表名(根据情况你自己修改一下)
select *
from (
select id,title,content,click,type,addTime,
ROW_NUMBER() OVER (PARTITION BY type ORDER BY click) number
from table
) temp
where number<=2
如果是sql server 2005的话,应该可以这样操作,自己没试,呵