以下是引用烟雨厦江南在2008-9-2 10:51:43的发言:第三个问题,在EXCEL中用SQL解决估计 那是相当的难,期待有这样的代码 Sub fig() With Worksheets("连续3天失败标记") Set CNN = CreateObject("adodb.connection") CNN.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;hdr=yes;';Data Source=" & ThisWorkbook.FullName .Range("f2:i1000").ClearContents r = .Range("a65536").End(xlUp).Row Sql = "select a.num,a.operation,a.dt,a.errtype FROM [连续3天失败标记$a1:d888] a,[连续3天失败标记$a1:d888] b,[连续3天失败标记$a1:d888] c where a.num=b.num and a.num=c.num and b.num=c.num and a.operation=b.operation and a.operation=c.operation and b.operation=c.operation and day(b.dt)-day(a.dt)=1 and day(c.dt)-day(b.dt)=1 and day(c.dt)-day(a.dt)=2" Sql = Sql & " union (select b.num,b.operation,b.dt,b.errtype FROM [连续3天失败标记$a1:d888] a,[连续3天失败标记$a1:d888] b,[连续3天失败标记$a1:d888] c where a.num=b.num and a.num=c.num and b.num=c.num and a.operation=b.operation and a.operation=c.operation and b.operation=c.operation and day(b.dt)-day(a.dt)=1 and day(c.dt)-day(b.dt)=1 and day(c.dt)-day(a.dt)=2)" Sql = Sql & " union (select c.num,c.operation,c.dt,c.errtype FROM [连续3天失败标记$a1:d888] a,[连续3天失败标记$a1:d888] b,[连续3天失败标记$a1:d888] c where a.num=b.num and a.num=c.num and b.num=c.num and a.operation=b.operation and a.operation=c.operation and b.operation=c.operation and day(b.dt)-day(a.dt)=1 and day(c.dt)-day(b.dt)=1 and day(c.dt)-day(a.dt)=2)" .Range("f2").CopyFromRecordset CNN.Execute(Sql) End With End Sub |