请教一条sql
cid | uid | title |ctime...
12 | 134 | abc |2007-06-06
13 | 102 | def |2007-06-06
12 | 102 | def |2007-06-06
13 | 102 | def |2007-06-06
15 | 102 | def |2007-06-06
15 | 102 | def |2007-06-06
13 | 102 | def |2007-06-06
.. ... ..
要求
按时间顺序 每个公司id(cid)只取一条记录(所有字段)取top N条记录
请sql高手帮忙 谢谢 啦
------解决方案--------------------Select Top N cid,uid,title,ctime From [YourTable] Group By cid Order By ctime Desc
这样只能做到选择N个记录,不能做到每公司一条
你这样有点复杂的需要用存储过程
------解决方案--------------------呵呵,忘排队了:
select cid,uid,title,time from(
select cid,uid,title,max(ctime) time from yourtable group by cid,uid,title
) where rownum < N order by time desc
------解决方案--------------------楼上的不用加order by time desc
已经用max(ctime)了
强烈怀疑楼上的楼上是否能达到一公司一记录的要求
------解决方案--------------------select top N * from article order by ctime DESC
------解决方案--------------------select * from food_comment where ctime in(SELECT max(ctime) as t FROM food_comment GROUP BY cid)
按时间顺序 每个公司id(cid)只取一条记录(所有字段)取top N条记录
-----------------------------------------------
这条SQL跟你文字表述的意图一致么?
------解决方案--------------------select top N * from TABLE as A where A.cid in (select top 1 cid from TABLE as B where B.cid=A.cid) order ty ctime