|
全部代码为:- Sub sqltest()
-
- Set Cnn = CreateObject("ADODB.Connection")
- Cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;Hdr=No;';Data Source=" & ThisWorkbook.FullName
-
- sql_1 = "TRANSFORM FIRST(结果) SELECT NULL FROM (SELECT f1 AS 结果,f1&f2 AS 字段 FROM [数据$] UNION SELECT f2,f1&f2&f1 FROM [数据$]) GROUP BY 1 PIVOT 字段"
- Sheet2.Range("A8").CopyFromRecordset Cnn.Execute(sql_1)
-
- sql_2 = "TRANSFORM FIRST(结果) SELECT NULL FROM (SELECT 结果,INT((COUNT(辅助2)-1)/6) AS 列字段,(COUNT(辅助2)-1) MOD 6 AS 行字段 FROM (SELECT f1 AS 结果,f1&f2 AS 辅助1 FROM [数据$] UNION SELECT f2,f1&f2&f1 FROM [数据$])a,(SELECT f1&f2 AS 辅助2 FROM [数据$] UNION SELECT f1&f2&f1 FROM [数据$])b WHERE 辅助1>=辅助2 GROUP BY 结果,辅助1) GROUP BY 列字段 PIVOT 行字段"
- Sheet3.Range("L1").CopyFromRecordset Cnn.Execute(sql_2)
-
- sql_3 = "TRANSFORM FIRST(结果) SELECT NULL FROM (SELECT 结果,列字段1,COUNT(辅助2) AS 行字段 FROM (SELECT f1 AS 结果,f1&f2 AS 辅助1,f1 AS 列字段1 FROM [数据$] UNION SELECT f2,f1&f2&f1,f1 FROM [数据$])a,(SELECT f1&f2 AS 辅助2,f1 AS 列字段2 FROM [数据$] UNION SELECT f1&f2&f1,f1 FROM [数据$])b WHERE 辅助1>=辅助2 AND 列字段1=列字段2 GROUP BY 结果,列字段1,辅助1) GROUP BY 列字段1 PIVOT 行字段"
- Sheet4.Range("A8").CopyFromRecordset Cnn.Execute(sql_3)
-
- sql_4 = "TRANSFORM FIRST(结果) SELECT 列字段 FROM (SELECT a.f1 AS 列字段,a.f2 AS 结果,COUNT(b.f1) AS 行字段 FROM [数据$]a,[数据$]b WHERE a.f1=b.f1 AND a.f2>=b.f2 GROUP BY a.f1,a.f2) GROUP BY 列字段 PIVOT 行字段"
- Sheet5.Range("L1").CopyFromRecordset Cnn.Execute(sql_4)
-
- sql_5 = "TRANSFORM FIRST(结果) SELECT 列字段2 FROM (SELECT 列字段1,结果,列字段1 AS 列字段2,行字段 FROM (SELECT a.f1 AS 列字段1,a.f2 AS 结果,COUNT(b.f1) AS 行字段 FROM [数据$]a,[数据$]b WHERE a.f1=b.f1 AND a.f2>=b.f2 GROUP BY a.f1,a.f2) UNION SELECT f1&f1,NULL,NULL,1 FROM [数据$]) GROUP BY 列字段1,列字段2 PIVOT 行字段"
- Sheet6.Range("L1").CopyFromRecordset Cnn.Execute(sql_5)
-
- sql_6 = "TRANSFORM FIRST(结果) SELECT 列字段2 FROM (SELECT 列字段1,结果,列字段1 AS 列字段2,行字段 FROM (SELECT a.f1 AS 列字段1,a.f2 AS 结果,COUNT(b.f1) AS 行字段 FROM [数据$]a,[数据$]b WHERE a.f1=b.f1 AND a.f2>=b.f2 GROUP BY a.f1,a.f2) UNION SELECT f1&f1,NULL,""总共("" & Count(f2) & ""人)"",1 FROM [数据$] GROUP BY f1) GROUP BY 列字段1,列字段2 PIVOT 行字段"
- Sheet7.Range("L1").CopyFromRecordset Cnn.Execute(sql_6)
-
- sql_7 = "TRANSFORM FIRST(结果) SELECT NULL FROM (SELECT a.f1 AS 列字段1,a.f2 AS 结果,(COUNT(b.f1)-1) MOD 3 +2 AS 行字段,INT((COUNT(b.f1)+2)/3) AS 列字段2 FROM [数据$]a,[数据$]b WHERE a.f1&a.f2>=b.f1&b.f2 AND a.f1=b.f1 GROUP BY a.f1,a.f2 UNION SELECT f1,f1,1,1 FROM [数据$]) GROUP BY 列字段1,列字段2 PIVOT 行字段"
- Sheet8.Range("F1").CopyFromRecordset Cnn.Execute(sql_7)
-
- sql_8 = "TRANSFORM FIRST(结果) SELECT NULL FROM (SELECT a.f1 AS 列字段1,a.f2 AS 结果,(COUNT(b.f1)-1) MOD 3 +2 AS 行字段,INT((COUNT(b.f1)-1)/3) AS 列字段2 FROM [数据$]a,[数据$]b WHERE a.f1&a.f2>=b.f1&b.f2 and a.f1=b.f1 GROUP BY a.f1,a.f2 UNION SELECT a.f1,a.f1,1,INT((COUNT(b.f1)-1)/3) FROM [数据$]a,[数据$]b WHERE a.f1&a.f2>=b.f1&b.f2 AND a.f1=b.f1 GROUP BY a.f1,a.f2) GROUP BY 列字段1,列字段2 PIVOT 行字段"
- Sheet9.Range("F1").CopyFromRecordset Cnn.Execute(sql_8)
-
- sql_9 = "TRANSFORM FIRST(结果) SELECT NULL FROM (SELECT a.f1 AS 列字段1,a.f2 AS 结果,(COUNT(b.f1)-1) MOD 3 +2 AS 行字段,INT((COUNT(b.f1)+2)/3) AS 列字段2 FROM [数据$]a,[数据$]b WHERE a.f1&a.f2>=b.f1&b.f2 AND a.f1=b.f1 GROUP BY a.f1,a.f2 UNION SELECT f1,f1,1,1 FROM [数据$] UNION SELECT f1,'总共('&COUNT(f1)&'人)',1,2 FROM [数据$] GROUP BY f1) GROUP BY 列字段1,列字段2 PIVOT 行字段"
- Sheet10.Range("F1").CopyFromRecordset Cnn.Execute(sql_9)
-
-
- Cnn.Close
- Set Cnn = Nothing
- End Sub
复制代码 |
|