-- 作者:刘林
-- 发布时间:2018/1/25 17:17:00
-- 排序效率
Dim ksmc As String = e.Form.Controls("combobox1").text If ksmc > "" Dim dt As DataTable = DataTables("成绩") Dim qArys As List(Of String()) = dt.GetValues("考试名称|年级代码") Dim st1 As Date = Date.Now If js.Contains("区业务主管") For Each qAry As String() In qArys Dim drs As List(Of DataRow) = dt.S e lect("[考试名称] = \'" & qary(0) & "\'And [年级代码] =\'" & qary(1) & "\' and 学段 =\'初中\'", "折总 DESC") For n As Integer = 0 To drs.Count - 1 \'遍历所有行 If n > 0 AndAlso drs(n)("折总") = drs(n-1)("折总") Then \'如果总分和上一行相同 drs(n)("区序") = drs(n-1)("区序") \'则排名等于上一行 Else drs(n)("区序") = n + 1 \'设置排名 End If Next Next For Each qAry As String() In qArys Dim drs1 As List(Of DataRow) = dt.Se lec t("[考试名称] = \'" & qary(0) & "\'And [年级代码] =\'" & qary(1) & "\'and 学段=\'初中\'", "总分 DESC") For n As Integer = 0 To drs1.Count - 1 \'遍历所有行 If n > 0 AndAlso drs1(n)("总分") = drs1(n-1)("总分") Then \'如果总分和上一行相同 drs1(n)("总分区序") = drs1(n-1)("总分区序") \'则排名等于上一行 Else drs1(n)("总分区序") = n + 1 \'设置排名 End If Next Next End If Dim Arys As List(Of String()) = dt.GetValues("考试名称|单位名称|年级代码") For Each Ary As String() In Arys Dim drs As List(Of DataRow) = dt.Sel e ct("[考试名称] = \'" & ary(0) & "\'And [单位名称] = \'" & ary(1) & "\'and [年级代码] =\'" & ary(2) & "\'and 学段=\'初中\'", "折总 DESC") For n As Integer = 0 To drs.Count - 1 \'遍历所有行 If n > 0 AndAlso drs(n)("折总") = drs(n-1)("折总") Then \'如果总分和上一行相同 drs(n)("校序") = drs(n-1)("校序") \'则排名等于上一行 Else drs(n)("校序") = n + 1 \'设置排名 End If Next Next For Each Ary As String() In Arys Dim drs As List(Of DataRow) = dt.Se le ct("[考试名称] = \'" & ary(0) & "\'And [单位名称] = \'" & ary(1) & "\'and [年级代码] =\'" & ary(2) & "\'and 学段 = \'初中\'", "总分 DESC") For n As Integer = 0 To drs.Count - 1 \'遍历所有行 If n > 0 AndAlso drs(n)("总分") = drs(n-1)("总分") Then \'如果总分和上一行相同 drs(n)("总分校序") = drs(n-1)("总分校序") \'则排名等于上一行 Else drs(n)("总分校序") = n + 1 \'设置排名 End If Next Next Dim Arys1 As List(Of String()) = dt.GetValues("考试名称|单位名称|年级代码|班级") For Each Ary1 As String() In Arys1 Dim drs As List(Of DataRow) = dt.Select("[考试名称] = \'" & ary1(0) & "\'And [单位名称] = \'" & ary1(1) & "\'and [年级代码] =\'" & ary1(2) & "\'And [班级] = \'" & ary1(3) & "\'and 学段=\'初中\'", "折总 DESC") For m As Integer = 0 To drs.Count - 1 \'遍历所有行 If m > 0 AndAlso drs(m)("折总") = drs(m-1)("折总") Then \'如果总分和上一行相同 drs(m)("班序") = drs(m-1)("班序") \'则排名等于上一行 Else drs(m)("班序") = m + 1 \'设置排名 End If Next Next For Each Ary1 As String() In Arys1 Dim drs As List(Of DataRow) = dt.Se le ct("[考试名称] = \'" & ary1(0) & "\'And [单位名称] = \'" & ary1(1) & "\'and [年级代码] =\'" & ary1(2) & "\'And [班级] = \'" & ary1(3) & "\'and 学段=\'初中\'", "总分 DESC") For m As Integer = 0 To drs.Count - 1 \'遍历所有行 If m > 0 AndAlso drs(m)("总分") = drs(m-1)("总分") Then \'如果总分和上一行相同 drs(m)("总分班序") = drs(m-1)("总分班序") \'则排名等于上一行 Else drs(m)("总分班序") = m + 1 \'设置排名 End If Next Next MessageBox.Show("亲,我正在为节省您的每一秒钟努力,本次保存共耗时: " & (Date.Now - st1).TotalSeconds & "秒") For Each r As Row In Tables("初中明细_table1") DataTables("成绩").SQLReplaceFor("班序", r("班序"), "[_Identify] = \'" & r("_Identify") & "\'") DataTables("成绩").SQLReplaceFor("校序", r("校序") ,"[_Identify] = \'" & r("_Identify") & "\'") DataTables("成绩").SQLReplaceFor("区序", r("区序") ,"[_Identify] = \'" & r("_Identify") & "\'") DataTables("成绩").SQLReplaceFor("总分班序", r("总分班序"), "[_Identify] = \'" & r("_Identify") & "\'") DataTables("成绩").SQLReplaceFor("总分校序", r("总分校序"), "[_Identify] = \'" & r("_Identify") & "\'") DataTables("成绩").SQLReplaceFor("总分区序", r("总分区序"), "[_Identify] = \'" & r("_Identify") & "\'") Next DataTables("成绩").load
Else messagebox.show("请选择要排的考试名称","提示") End If
老师,因为一次考试有2万个记录,加载出来排序,因为有6列排序,排好后用save保存要花很多时间,用sql提取保存也很久,我想用sqlreplacefor的办法结果简直一直等,请老师帮忙看一下如何做更高效,一二十秒能搞定也好嘛
|
-- 作者:有点甜
-- 发布时间:2018/1/25 17:54:00
--
看了一下,你的排名,根本就不需要保存。你尝试执行这个sql语句,看执行需要多长时间(自己加上where条件)
select rank() over(order by 折总 desc) as 折总排名, rank() over(order by 总分 desc) as 总分排名, * from {成绩}
如果执行时间不长,那就每次动态计算呗。
如果确实需要存储。你可以用update语句,如
update a set a.第五列=b.折总排名, a.第六列=b.总分排名 from {成绩} a, (select rank() over(order by 折总 desc) as 折总排名, rank() over(order by 总分 desc) as 总分排名, * from {成绩}) b where a.[_Identify]=b.[_Identify]
|
-- 作者:有点甜
-- 发布时间:2018/1/25 18:19:00
--
以下是引用刘林在2018/1/25 18:09:00的发言:
还有我是要排六列数据,班序,校序,区序,总分班序,总分校序,总分区序,如何写?sql理解不到
看懂红色代码,拷贝多段,去sql测试窗口测试
select rank() over(partition by 考试名称,年级代码 order by 折总 desc) as 区折总排名, rank() over(partition by 考试名称,年级代码,单位名称,班级 order by 折总 desc) as 班折总排名, * from {成绩}
如果不会写,贴出数据库/表格信息测试。
|