「代码」「SQL法」提取各科成绩第一名的记录

文摘   教育   2024-10-23 22:50   江苏  

点【关于本公众号】了解一下,欢迎关注谢谢!

快速浏览

实用案例

|日期控件||简单的收发存||收费管理系(Access改进版)|

|电子发票管理助手||电子发票登记系统(Access版)|

|文件合并||表格拆分||审计凭证抽查底稿|

|中医诊所收费系统(Excel版)||中医诊所收费系统(Access版)||银行对账单自动勾对|

|印章使用登记系统|

收费使用项目

|财务管理系统||工资薪金和年终奖个税筹划||新税法下工资表模版|

内容提要

  • 提取各科成绩第一名(SQL法)|完整代码
1、在工作表“成绩”里,命令按钮点击事件,调用相应过程:
Private Sub CmdFirstSQL_Click()    Call getFirstSQLEnd Sub
2、在myModule里,getFirstSQL过程,提取各科第一名成绩的记录:
第一部分、定义变量,建立数据库连接,编写SQL查询语句,这里的SQL语句还可以通过循环表头科目字段动态生成,科目名称变化就不受影响了:
Sub getFirstSQL()    Dim conn As Object    Dim rs As Object    Dim sql As String    Dim row As Long, firstCol As Integer        '创建 ADO 连接    Set conn = CreateObject("ADODB.Connection")    Set rs = CreateObject("ADODB.Recordset")            '使用 OLEDB 连接 Excel 文件    conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.FullName & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"    conn.Open        'SQL 查询每个学科的最高分    sql = "SELECT '语文' AS 学科, [准考证号], [姓名], [班级], [语文] AS 分数 FROM [成绩$] WHERE [语文] = (SELECT MAX([语文]) FROM [成绩$]) " & _        "UNION " & _        "SELECT '数学' AS 学科, [准考证号], [姓名], [班级], [数学] AS 分数 FROM [成绩$] WHERE [数学] = (SELECT MAX([数学]) FROM [成绩$]) " & _        "UNION " & _        "SELECT '英语' AS 学科, [准考证号], [姓名], [班级], [英语] AS 分数 FROM [成绩$] WHERE [英语] = (SELECT MAX([英语]) FROM [成绩$]) " & _        "UNION " & _        "SELECT '物理' AS 学科, [准考证号], [姓名], [班级], [物理] AS 分数 FROM [成绩$] WHERE [物理] = (SELECT MAX([物理]) FROM [成绩$]) " & _        "UNION " & _        "SELECT '化学' AS 学科, [准考证号], [姓名], [班级], [化学] AS 分数 FROM [成绩$] WHERE [化学] = (SELECT MAX([化学]) FROM [成绩$]) " & _        "UNION " & _        "SELECT '政治' AS 学科, [准考证号], [姓名], [班级], [政治] AS 分数 FROM [成绩$] WHERE [政治] = (SELECT MAX([政治]) FROM [成绩$]) " & _        "UNION " & _        "SELECT '历史' AS 学科, [准考证号], [姓名], [班级], [历史] AS 分数 FROM [成绩$] WHERE [历史] = (SELECT MAX([历史]) FROM [成绩$]) " & _        "UNION " & _        "SELECT '生物' AS 学科, [准考证号], [姓名], [班级], [生物] AS 分数 FROM [成绩$] WHERE [生物] = (SELECT MAX([生物]) FROM [成绩$]) " & _        "UNION " & _        "SELECT '地理' AS 学科, [准考证号], [姓名], [班级], [地理] AS 分数 FROM [成绩$] WHERE [地理] = (SELECT MAX([地理]) FROM [成绩$]) " & _        "UNION " & _        "SELECT '体育' AS 学科, [准考证号], [姓名], [班级], [体育] AS 分数 FROM [成绩$] WHERE [体育] = (SELECT MAX([体育]) FROM [成绩$]) " & _        "order by 学科 DESC"
第二部分、执行查询,把记录集写入目标工作表
    '执行 SQL 查询    'rs.Open sql, conn        Set rs = conn.Execute(sql)        '在新工作表中输出查询结果    With Sheet2        firstCol = 6        '输出表头        .Cells(1, firstCol + 1).Value = "准考证号"        .Cells(1, firstCol + 2).Value = "姓名"        .Cells(1, firstCol + 3).Value = "班级"        .Cells(1, firstCol + 4).Value = "学科"        .Cells(1, firstCol + 5).Value = "分数"                '输出查询结果        'row = 2        'Do Until rs.EOF            '.Cells(row, firstCol + 1).Value = rs.Fields("准考证号").Value            '.Cells(row, firstCol + 2).Value = rs.Fields("姓名").Value            '.Cells(row, firstCol + 3).Value = rs.Fields("班级").Value            '.Cells(row, firstCol + 4).Value = rs.Fields("学科").Value            '.Cells(row, firstCol + 5).Value = rs.Fields("分数").Value            'row = row + 1            'rs.MoveNext        'Loop                '不用循环记录集,直接用CopyFromRecordset方法        .Cells(2, firstCol + 1).CopyFromRecordset rs    End With        '关闭记录集和连接    rs.Close    conn.Close        MsgBox "提取各科第一名数据已完成!"End Sub
~~~~~~End~~~~~~

安利小店
安利的牙膏非常不错,用了以后就不想再用其他的了;洗洁精洗衣液也是日常必备,用过都说好!

合谷医疗
合谷医疗专攻各种疑难杂症,尤其擅长抑郁症焦虑失眠儿童神经发育异常多动症自闭孤独症腰颈椎疾病治疗,可谓神乎其技!体验过的直呼早点来就好了

喜欢就点个、点在看留言评论、分享一下呗!感谢支持!

  • Excel问题,请在文章下面留言讨论!或者加入我的付费交流群提问

  • 如需案例文件,请按当天另一篇文章末尾案例文件分享说明操作!

VBA编程实战
Excel应用案例、Excel VBA、公式函数使用技巧分享,思路解读...... 这里有鲜活案例、实用的技巧......
 最新文章