一句SQL,求优化 、求调教~~!
求教给位达人:
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 = 1order 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 = 11、首先 把你的 * 换成具体的字段。2、第二个 * 改用一个字段(CARID)或直接写 13、就是在你的条件上 建索引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=1order 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
[解决办法]
--tryselect * 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/*这样应该可以了*/