急需一条sql语句,在线等
从spzs表总共取5条信息(id,cpmc,dateandtime,gsid,zsbz),在hot_top里设置了gsid,可能是多个gsid,优先取hot_top表中gsid的日期最新的一条信息,不重复,不足5条的,再取其他gsid的不重复的一条信息,凑足5条就可以了,我做了如下语句,但只取了id号最大的,不能满足需要,现在需要修改为取日期最新的:
sql="select top 5 b.id,b.cpmc,b.dateandtime,b.gsid,b.zsbz from (select max(id) id from spzs where flag=1 group by gsid) a inner join spzs b on a.id=b.id left join hot_top c on b.gsid=c.gsid order by case when c.gsid is not null then 0 when b.zsbz like '%02% ' then 1 else 2 end,b.dateandtime desc"
请改为取日期最新的SQL
[解决办法]
select top 5 b.id,b.cpmc,b.dateandtime,b.gsid,b.zsbz from spzs a inner join spzs b on a.id=b.id left join hot_top c on b.gsid=c.gsid where not exists(select 1 from spzs where flag=a.flag and gsid=a.gsid and flag=1 and dateandtime>a.dateandtime)order by case when c.gsid is not null then 0 when b.zsbz like '%02% ' then 1 else 2 end, b.dateandtime desc
[解决办法]
复杂,真复杂,干吗搞那么复杂啊~~~~~~~~~~
[解决办法]
楼主,最好把表写出来。看看。
具体点。谢谢。
[解决办法]
select a.* from spzs a where dateandtime = (select max(dateandtime) from spzs where gsid = a.gsid)至于top 5,你自己选择一个排序,如对gsid排序select top 5 * from (select a.* from spzs a where dateandtime = (select max(dateandtime) from spzs where gsid = a.gsid)) t