统计出表中或数组中每个重复项出现的次数

下午HR的同事拿来一个上万的数据,要求对表中出现的人员进行次数统计,起先要求是用一个asp.net程序来实现但我想应该可以用一句SQL语句来实现的.

step
1.由于是一个文本文件,我把他导入至SQL server中一个数据库中
2.打开查询分析器
一句:

select col001,count(col001) from name group by col001 //

如果要在查询出来的字段命名则


select col001,Appearnum=count(col001) from name group by col001


就可以出现了

在网上查询相关资料,发现一个贴子很火(应当是从CSDN copy过来的)
http://access911.net/getnews/doc1.asp?mode=a&aid=4708185

里面的高手如去,各种方法都有了

梅花雪的:javascript版








jsadmin的ASP版:



<%
startime=timer()
%>

<%
dim arr : arr=Array(1,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,1,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,1,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,1,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,1,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,1,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,1,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,1,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,1,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3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ero=0
for each xxx in dic
if i=0 then
numhign=dic(xxx)
num=xxx
else
if dic(xxx)>numhign then
numhign=dic(xxx)
num=xxx
zero=0
elseif dic(xxx)=numhign then
zero=1
end if
end if
i=i+1
next
if zero=1 then
alert(0)
else
response.write "出现频率最大的数是:" & replace(num,"dic_","")
end if
%>

<%
endtime=timer()
response.write "
执行时间:"&FormatNumber((endtime-startime)*1000,3)&"毫秒"
%>




fashon的正则版






longfeisoft的vb版


[VISUAL BASIC]
Private Sub Command1_Click()
    Dim col As New Collection
    Dim s() As String
    Dim i As Long
    Dim j As Long
    Dim itemStr  As String
    Dim str As String
    
    s = Split(Text1.Text, ",")
    For i = 0 To UBound(s)
        j = ExistCol(col, s(i))
        If j > 0 Then
            col.Remove (s(i))
        End If
        col.Add (j + 1) & "---" & s(i), s(i)
    Next
    j = 0
    For i = 0 To col.Count
        If Val(col.Item(s(i))) > j Then
            j = Val(col.Item(s(i)))
            itemStr = col.Item(s(i))
            str = str & vbCrLf & itemStr & ":" & j
        End If
    Next
    MsgBox str & vbCrLf & "max times:" & itemStr
End Sub

Function ExistCol(col As Collection, key As String)
On Error GoTo FuncErr
    ExistCol = Val(col.Item(key))
    Exit Function
FuncErr:
    ExistCol = 0
End Function




qzriso的PHP版


$time_start=explode(" ",microtime());
$a=array(1,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,1,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,1,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,1,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,1,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,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4,3,2,1,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,1,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2,1,5,2,3,4,5,6,3,7,9,8,4,3,2,5,2,3,4,5,6,3,7,9,8,4,3,2);


$c=array_count_values($a);
$m=0;
for($i=0;$i  if($m<$c[$i]){$m=$c[$i];$ipos=$i;}
}
$key=array_search($c[$ipos],$c);
print("出现频率最高的数是:".$key."  出现次数:".$c[$ipos]);
$time_end=explode(" ",microtime());
$t=$time_end[0]+$time_end[1]-$time_start[0]-$time_start[1];
print("

运行时间:".$t."秒");





laifangsong的C#版

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace suanfa
{
///
/// FindMaxTimes 的摘要说明。
///

public class FindMaxTimes : System.Web.UI.Page
{
private const int max = 16;
private int[] arrNumbers = {1,3,2,2,3,2,2,1,3,2,2,4,5,6,7,8};
private int[,] arrAppearTimes = new int[max,2];
private int curBound = 0;
private int maxBound = 0;
private void Page_Load(object sender, System.EventArgs e)
{
SetAppearTime();
Response.Write("每个数字出现次数分别是:
");
ListAppearTimes();
Response.Write("出现次数最多的数字是:
");
Response.Write(arrAppearTimes[maxBound,0] + "    " + arrAppearTimes[maxBound,1]);

}
private void SetAppearTime()
{
for(int i=0; i{
int appearBound = IndexInAppearTimes(arrNumbers[i]);
if(appearBound != -1)
{
arrAppearTimes[appearBound,1] += 1;
}
else
{
arrAppearTimes[curBound,0] = arrNumbers[i];
arrAppearTimes[curBound,1]  = 1;
curBound++;
}
}
}
private void ListAppearTimes()
{
for(int i=0; i{
Response.Write(arrAppearTimes[i,0] + "    " + arrAppearTimes[i,1] + "
");
if(arrAppearTimes[i,1] > arrAppearTimes[maxBound,1])
{
maxBound = i;
}
}
}
private int IndexInAppearTimes(int num)
{
for(int i=0; i{
if(arrAppearTimes[i,0] == num)
{
return i;
}
}
return -1;
}


#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///

private void InitializeComponent()
{    
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}



太强了,大家认真看下这贴吧




[本日志由 admin 于 2008-05-10 11:05 PM 编辑]
上一篇: [译]在ASP.NET2.0和Visual Studio2005中操作数据库
下一篇: ASP.NET学习之对文件和文件夹的操作
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 139 | 查看次数: 49618
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 30 字 | UBB代码 关闭 | [img]标签 关闭