ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 210|回复: 4

如何SQL中的MAX(IIf(Path LIKE '%\%', Path, NULL))应用??

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-6-28 09:44 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助


谢谢MR_PP的回帖。
在如何将Group by和Where写在一个SQL中,实现目标结果。-Excel VBA程序开发-ExcelHome技术论坛 -  https://club.excelhome.net/thread-1693948-1-1.html

到现在不理解在SQL中的MAX(IIf(Path LIKE '%\%', Path, NULL))应用

          "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 " & _


image.png



  1. Function SqlRetuRs(Str As String) As ADODB.Recordset
  2.     Dim Cn As ADODB.Connection
  3.     Set Cn = New ADODB.Connection
  4.     Dim rs As ADODB.Recordset
  5.     Set rs = New ADODB.Recordset
  6.    
  7.     If InStr(UCase(Application.Path), "WPS") > 0 Then
  8.         Cn.Open "Provider=Microsoft.JET.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=yes';Data Source=" & ThisWorkbook.FullName
  9.     Else
  10.         Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=yes';Data Source=" & ThisWorkbook.FullName
  11.     End If
  12.    
  13.     rs.Open Str, Cn, adOpenKeyset, adLockOptimistic
  14.     Set SqlRetuRs = rs
  15.    
  16. End Function

  17. Sub Report()
  18.     Dim Str As String
  19.     Dim Sht As Worksheet
  20.     Dim rs As ADODB.Recordset
  21.    
  22.     Set Sht = ThisWorkbook.Sheets("Sheet1")
  23.     Sht.Range("A15:I100").Clear ' 清除结果
  24.    
  25.     Str = "SELECT  Name, COUNT(Name) AS Num, " & _
  26.           "IIf(COUNT(Name) > 0, MAX(IIf(Path LIKE '%\%', Path, NULL)), NULL) AS Path, " & _
  27.           "IIf(COUNT(Name) > 1, MAX(IIf(Path LIKE '%\%\%\%', Path, NULL)), NULL) AS Path1, " & _
  28.           "IIf(COUNT(Name) > 2, MAX(IIf(Path LIKE 'F:\A\B\C\D\%', Path, NULL)), NULL) AS Path2 " & _
  29.           "FROM [Sheet1$J13:L889] " & _
  30.           "WHERE Name IS NOT NULL " & _
  31.           "GROUP BY  Name " & _
  32.           "ORDER BY  Name" 'SQL查询语句
  33.    
  34.     Set rs = SqlRetuRs(Str)
  35.     If Not rs.EOF Then '输出
  36.         Sht.Cells(15, 1).CopyFromRecordset rs
  37.     End If
  38. End Sub
复制代码


a.rar

54.02 KB, 下载次数: 8

a - 副本.rar

54.04 KB, 下载次数: 4

TA的精华主题

TA的得分主题

发表于 2024-6-28 10:52 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
iif(条件,条件成立的情况,条件不成立的情况)

TA的精华主题

TA的得分主题

发表于 2024-6-28 10:55 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
MAX(1,2)  取大的部分
MAX(IIf(Path LIKE '%\%', A,B)), C)
如果内部iif条件为真 就是MAX(A,C)  否则就是 MAX(B,C)

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-6-29 08:31 | 显示全部楼层
wang-way 发表于 2024-6-28 10:55
MAX(1,2)  取大的部分
MAX(IIf(Path LIKE '%\%', A,B)), C)
如果内部iif条件为真 就是MAX(A,C)  否则就 ...

谢谢解答。

"SELECT  Name,MAX(IIf(Path LIKE '%\%', Path, NULL)) FROM [Sheet1$X13:Z889]
Max的语句 MAX(IIf(Path LIKE '%\%', Path, NULL))不成立。
image.png

不懂这条语句,结果不对。
IIf(COUNT(Name) > 2, MAX(IIf(Path LIKE '%\%\%\%', Path, NULL))


image.png


  1. Function SqlRetuRs(Str As String) As ADODB.Recordset
  2.     Dim Cn As ADODB.Connection
  3.     Set Cn = New ADODB.Connection
  4.     Dim rs As ADODB.Recordset
  5.     Set rs = New ADODB.Recordset
  6.    
  7.     If InStr(UCase(Application.Path), "WPS") > 0 Then
  8.         Cn.Open "Provider=Microsoft.JET.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=yes';Data Source=" & ThisWorkbook.FullName
  9.     Else
  10.         Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=yes';Data Source=" & ThisWorkbook.FullName
  11.     End If
  12.    
  13.     rs.Open Str, Cn, adOpenKeyset, adLockOptimistic
  14.     Set SqlRetuRs = rs
  15.    
  16. End Function

  17. Sub Report()
  18.     Dim Str As String
  19.     Dim Sht As Worksheet
  20.     Dim rs As ADODB.Recordset
  21.    
  22.     Set Sht = ThisWorkbook.Sheets("Sheet1")
  23.     Sht.Range("A15:I100").Clear ' 清除结果
  24.    
  25.     Str = "SELECT  Name, COUNT(Name) AS Num, " & _
  26.           "IIf(COUNT(Name) > 0, MAX(IIf(Path LIKE '%\%', Path, NULL)), NULL) AS Path, " & _
  27.           "IIf(COUNT(Name) > 1, MAX(IIf(Path LIKE '%\%\%\%', Path, NULL)), NULL) AS Path1, " & _
  28.           "IIf(COUNT(Name) > 2, MAX(IIf(Path LIKE 'F:\A\B\C\D\%', Path, NULL)), NULL) AS Path2 " & _
  29.           "FROM [Sheet1$J13:L889] " & _
  30.           "WHERE Name IS NOT NULL " & _
  31.           "GROUP BY  Name " & _
  32.           "ORDER BY  Name" 'SQL查询语句
  33.    
  34.     'Debug.Print Str
  35.     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"
  36.     Str = "SELECT  Name,Path LIKE '%\%\%\%\%',IIf(Path LIKE '%\%\%', Path, NULL), Path LIKE '%\%', IIf(Path LIKE '%\%', Path, NULL) FROM [Sheet1$X13:Z889] Group By Name "
  37.     Str = "SELECT  Name,Count(Name),IIf(Path LIKE '%\%\%\%', Path, NULL) FROM [Sheet1$X13:Z889] Group By Name "
  38.     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 "
  39.     Debug.Print Str
  40.     Set rs = SqlRetuRs(Str)
  41.     If Not rs.EOF Then '输出
  42.         Sht.Cells(15, 1).CopyFromRecordset rs
  43.     End If
  44. End Sub


复制代码


TA的精华主题

TA的得分主题

 楼主| 发表于 2024-6-29 15:55 | 显示全部楼层

花了将近一个月时间,下面程序实在不会用,
Path LIKE '%\%'??
IIf(Path LIKE '%\%', Path, NULL)??
MAX(IIf(Path LIKE '%\%', Path, NULL))??????


          "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 " & _





  1. Function SqlRetuRs(Str As String) As ADODB.Recordset
  2.     Dim Cn As ADODB.Connection
  3.     Set Cn = New ADODB.Connection
  4.     Dim Rs As ADODB.Recordset
  5.     Set Rs = New ADODB.Recordset
  6.    
  7.     If InStr(UCase(Application.Path), "WPS") > 0 Then
  8.         Cn.Open "Provider=Microsoft.JET.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=yes';Data Source=" & ThisWorkbook.FullName
  9.     Else
  10.         Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=yes';Data Source=" & ThisWorkbook.FullName
  11.     End If
  12.    
  13.     Rs.Open Str, Cn, adOpenKeyset, adLockOptimistic
  14.     Set SqlRetuRs = Rs
  15.    
  16. End Function

  17. Sub Report()
  18.     Dim Str As String
  19.     Dim Sht As Worksheet
  20.     Dim Rs As ADODB.Recordset
  21.    
  22.     Set Sht = ThisWorkbook.Sheets("Sheet1")
  23.     Sht.Range("A15:I100").Clear ' 清除结果
  24.    
  25.     Str = "SELECT  Name, COUNT(Name) AS Num, " & _
  26.           "IIf(COUNT(Name) > 0, MAX(IIf(Path LIKE '%\%', Path, NULL)), NULL) AS Path, " & _
  27.           "IIf(COUNT(Name) > 1, MAX(IIf(Path LIKE '%\%\%\%', Path, NULL)), NULL) AS Path1, " & _
  28.           "IIf(COUNT(Name) > 2, MAX(IIf(Path LIKE 'F:\A\B\C\D\%', Path, NULL)), NULL) AS Path2 " & _
  29.           "FROM [Sheet1$J13:L889] " & _
  30.           "WHERE Name IS NOT NULL " & _
  31.           "GROUP BY  Name " & _
  32.           "ORDER BY  Name" 'SQL查询语句
  33.    
  34.     Set Rs = SqlRetuRs(Str)
  35.     If Not Rs.EOF Then '输出
  36.         Sht.Cells(15, 1).CopyFromRecordset Rs
  37.     End If
  38. End Sub

复制代码
做了很长时间的无用功,有回到最初最简单的方法。





  1. Sub mm()

  2.     Dim Str As String, SqlStr
  3.     Dim Sht As Worksheet
  4.     Dim Rs As ADODB.Recordset
  5.    
  6.     Set Sht = ThisWorkbook.Sheets("Sheet4")
  7.     Sht.Range("A15:I100").Clear ' 清除结果
  8.     Dim Rng As Range, R1 As Range, R2 As Range
  9.         Set R1 = Sht.Range("Q13:S84")
  10.     Dim SqlShtStr
  11.         SqlShtStr = " From [" & Sht.Name & "$" & R1.Address(0, 0) & "]"
  12.         Str = "Select Distinct Name " & SqlShtStr & " Where Not Name Is  Null"
  13.         Debug.Print Str
  14.         Set Rs = SqlRetuRs(Str)
  15.         If Not Rs.EOF Then '输出
  16.             Sht.Cells(15, 1).CopyFromRecordset Rs
  17.         End If
  18.         Set Rng = Sht.Cells(15, 1).Resize(Rs.RecordCount, 2)
  19.         ''
  20.         For ii = 1 To Rng.Rows.Count
  21.             RepeatFileToRng Rng(ii, 3), Rng(ii, 1), SqlShtStr
  22.         Next ii
  23. End Sub


  24. Function RepeatFileToRng(Rng As Range, Str, SqlShtStr)
  25.       Dim SqlStr As String
  26.           SqlStr = "Select Path,Name " & SqlShtStr & " Where Name = '" & Str & "'"

  27.       Dim Rs As ADODB.Recordset
  28.           Set Rs = SqlRetuRs(SqlStr)
  29.           With Rs
  30.             .MoveFirst
  31.             For jj = 1 To Rs.RecordCount
  32.                  'Rng(, jj) = Rs.Fields(0)
  33.                  'Rng(, jj).Hyperlinks.Add .Fields(0), .Fields(0)
  34.                  Rng(, jj).Hyperlinks.Add Rng(, jj), .Fields(0)
  35.                  .MoveNext
  36.             Next jj
  37.           End With
  38.          
  39. End Function
复制代码


您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-17 21:45 , Processed in 0.028853 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表