日期:2014-05-18 浏览次数:20646 次
select * from Table a where exists(select * from Table where a.CARID =CARID having a.CREATETIME =max(CREATETIME) ) and LINEID ='302' and UPDOWN = 1
select top 1 * from Table a where LINEID ='302' and UPDOWN = 1 order by CREATETIME desc
------解决方案--------------------
select * from Table a where exists(select * from Table where a.CARID =CARID having a.CREATETIME =max(CREATETIME) ) and LINEID ='302' and UPDOWN = 1 1、首先 把你的 * 换成具体的字段。 2、第二个 * 改用一个字段(CARID)或直接写 1 3、就是在你的条件上 建索引 4、如果查询所得的列个数少,再建该列的 包含索引
------解决方案--------------------
select * from Table a where exists(select * from Table where a.CARID =CARID having a.CREATETIME =max(CREATETIME) ) and LINEID ='302' and UPDOWN = 1 --楼主为何写子查询呢 select --所有字段 from table a where createtime=max(createtieme) and lineid='302' and updown=1 order by CREATETIME desc
------解决方案--------------------
select * from Table a where
exists(select * from Table where a.CARID =CARID having a.CREATETIME =max(CREATETIME) )
and LINEID ='302' and UPDOWN = 1
select * from
(
select *,id=row_number () over (partition by CARID order by CREATETIME desc)
from table
where LINEID ='302' and UPDOWN = 1
) K
where id=1
------解决方案--------------------
--try select * from Table a where not exists(select * from Table where a.CARID =CARID and a.CREATETIME<CREATETIME ) and LINEID ='302' and UPDOWN = 1
------解决方案--------------------
select * from Table a where a.CREATETIME = (select max(b.CREATETIME) from Table b where a.CARID =b.CARID) and LINEID ='302' and UPDOWN = 1 /*这样应该可以了*/