|
楼主 |
发表于 2024-6-29 08:31
|
显示全部楼层
谢谢解答。
"SELECT Name,MAX(IIf(Path LIKE '%\%', Path, NULL)) FROM [Sheet1$X13:Z889]
Max的语句 MAX(IIf(Path LIKE '%\%', Path, NULL))不成立。
不懂这条语句,结果不对。
IIf(COUNT(Name) > 2, MAX(IIf(Path LIKE '%\%\%\%', Path, NULL))
- Function SqlRetuRs(Str As String) As ADODB.Recordset
- Dim Cn As ADODB.Connection
- Set Cn = New ADODB.Connection
- Dim rs As ADODB.Recordset
- Set rs = New ADODB.Recordset
-
- If InStr(UCase(Application.Path), "WPS") > 0 Then
- Cn.Open "Provider=Microsoft.JET.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=yes';Data Source=" & ThisWorkbook.FullName
- Else
- Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=yes';Data Source=" & ThisWorkbook.FullName
- End If
-
- rs.Open Str, Cn, adOpenKeyset, adLockOptimistic
- Set SqlRetuRs = rs
-
- End Function
- Sub Report()
- Dim Str As String
- Dim Sht As Worksheet
- Dim rs As ADODB.Recordset
-
- Set Sht = ThisWorkbook.Sheets("Sheet1")
- Sht.Range("A15:I100").Clear ' 清除结果
-
- Str = "SELECT Name, COUNT(Name) AS Num, " & _
- "IIf(COUNT(Name) > 0, MAX(IIf(Path LIKE '%\%', Path, NULL)), NULL) AS Path, " & _
- "IIf(COUNT(Name) > 1, MAX(IIf(Path LIKE '%\%\%\%', Path, NULL)), NULL) AS Path1, " & _
- "IIf(COUNT(Name) > 2, MAX(IIf(Path LIKE 'F:\A\B\C\D\%', Path, NULL)), NULL) AS Path2 " & _
- "FROM [Sheet1$J13:L889] " & _
- "WHERE Name IS NOT NULL " & _
- "GROUP BY Name " & _
- "ORDER BY Name" 'SQL查询语句
-
- 'Debug.Print Str
- Str = "SELECT Name,Path LIKE '%\%\%\%',IIf(Path LIKE '%\%\%\%', Path, NULL), Path LIKE '%\%', IIf(Path LIKE '%\%', Path, NULL) FROM [Sheet1$X13:Z889] WHERE Name IS NOT NULL"
- Str = "SELECT Name,Path LIKE '%\%\%\%\%',IIf(Path LIKE '%\%\%', Path, NULL), Path LIKE '%\%', IIf(Path LIKE '%\%', Path, NULL) FROM [Sheet1$X13:Z889] Group By Name "
- Str = "SELECT Name,Count(Name),IIf(Path LIKE '%\%\%\%', Path, NULL) FROM [Sheet1$X13:Z889] Group By Name "
- Str = "SELECT Name,Count(Name),MAX(IIf(Path LIKE '%\%', Path, NULL)),MAX(IIf(Path LIKE '%\%\%\%', Path, NULL)),MAX(IIf(Path LIKE '%\%\%\%\%\%\%\%', Path, NULL)) FROM [Sheet1$X13:Z889] Group By Name "
- Debug.Print Str
- Set rs = SqlRetuRs(Str)
- If Not rs.EOF Then '输出
- Sht.Cells(15, 1).CopyFromRecordset rs
- End If
- End Sub
复制代码
|
|