这个是我自己写的一条sql语句,对错还不知道,但运行效率太差,条件中的officecode,custid,oughtreaddate都为索引,数据库中有将近8000W条记录。<br>select distinct a.custid,a.oughtreaddate,a.watersum,a.prevmeterdata,a.meterdata,b.watersum,c.watersum<br>from readmeter a <br>left join (<br> select CustID,oughtreaddate,watersum from readmeter where (custid,oughtreaddate) in<br> (select CustID,max(oughtreadDate) <br> from Readmeter where officecode='02' and oughtreaddate <'01-9月-06' <br> and custid in (select custid from readmeter where officecode='02' and oughtreaddate between '01-9月-06' and '30-9月-06'<br>and viewtype='6')<br> group by custid)) b on a.custid=b.custid<br>left join (<br> select custid,oughtreaddate,watersum from readmeter where custid in<br> (select custid from readmeter where officecode='02' and oughtreaddate between '01-9月-05' and '30-9月-05' <br> and custid in (select custid from readmeter where officecode='02' and oughtreaddate between '01-9月-06' and '30-9月-06'<br>and viewtype='6'))<br> ) c on a.custid=c.custid<br>where a.officecode='02' and a.oughtreaddate between '01-9月-06' and '30-9月-06'<br>and a.viewtype='6'