当前位置:编程学习 > C#/ASP.NET >>

如何用C#统计字段值个数?

数据表名:Honor

ID  score       time
1     1         2009-11-5 17:30
2     1         2009-11-15 15:35   
3     -1        2009-11-16 16:30 
4     0         2009-10-16 16:30 

需要统计近一周、,近一个月、近三个月,得分为1,-1,0的数量

请问用C#如何实现啊,谢谢!
希望各位可详细指点一下啊.. --------------------编程问答-------------------- 用SQL可以很轻松解决的,涉及select count(*) where where diff(day,time,getdate())<=7(此处一个月就是30,三个月就是90)  --------------------编程问答-------------------- 补充一下,条件里还应加上and score=1(-1,0) --------------------编程问答--------------------

declare @t table( [id] int , score int , [time] datetime)

insert into @t values(1  ,  1    ,    '2009-11-5 17:30'   )
insert into @t values(2  ,  1    ,    '2009-11-15 15:35'  )
insert into @t values(3  ,  -1   ,     '2009-11-16 16:30' )
insert into @t values(4  ,  0    ,    '2009-10-16 16:30'  )

select score , count([id]) as num ,'周' as type   from @t   where DATEDIFF(day,time,getdate()) <=7 
group by score 
union all 
select score , count([id]) as num ,'月' as type   from @t   where DATEDIFF(day,time,getdate()) <=30 
group by score 
union all 
select score , count([id]) as num ,'三个月' as type   from @t   where DATEDIFF(day,time,getdate()) <=90 
group by score 
--------------------编程问答-------------------- 其他的自己修改一下!

            //统计近一周
            int i=0;
            int j = 0;
            int m = 0;
            foreach (DataRow d in dt.Rows)
            {
                if (DateTime.Compare(DateTime.Parse(d[3].ToString()), DateTime.Now) <= 7)
                {
                    switch (d[1].ToString())
                    {
                        case "1":
                            i++;
                            break;
                        case "0":
                            j++;
                            break;
                        case "-1":
                            m++;
                            break;
                    }
                }

            }
            Console.WriteLine("近一周内:1次数"+i.ToString()+"\n"+"0次数"+j.ToString()+"\n"+"-1次数"+i.ToString());
--------------------编程问答-------------------- TO xinke_li:

System.Data.SqlClient.SqlException: 'diff' 不是可以识别的 内置函数名称。 --------------------编程问答-------------------- 可能之前说得有点简单:


数据表名:Honor 

ID  score      time                  sales     buyer
1    1        2009-11-5 17:30        redhcy     hcy
2    1        2009-11-15 15:35       redhcy     bluehcy
33    -1        2009-11-16 16:30      hcy        wm
40    0        2009-10-16 16:30       hcy        wm
..............

score表示用户的评分  time表示评价时间  sales表示卖家   buyer表示买家
表中的行数是不断增加的,有用户评分,就增加一行数据。

需要统计并显示在aspx页面上:

卖家=lbluser.text,得分为1,一周内的评价数;
卖家=lbluser.text,得分为-1,一周内的评价数;
卖家=lbluser.text,得分为0,一周内的评价数;
卖家=lbluser.text,得分为1,一个月内的评价数;
卖家=lbluser.text,得分为-1,一个月内的评价数;
卖家=lbluser.text,得分为0,一个月内的评价数;
卖家=lbluser.text,得分为1,六个月内的评价数;
卖家=lbluser.text,得分为-1,六个月内的评价数;   
卖家=lbluser.text,得分为0,六个月内的评价数;

买家=lbluser.text,得分为1,一周内的评价数;
买家=lbluser.text,得分为-1,一周内的评价数;
买家=lbluser.text,得分为0,一周内的评价数;
买家=lbluser.text,得分为1,一个月内的评价数;
买家=lbluser.text,得分为-1,一个月内的评价数;
买家=lbluser.text,得分为0,一个月内的评价数;
买家=lbluser.text,得分为1,六个月内的评价数;
买家=lbluser.text,得分为-1,六个月内的评价数;   
买家=lbluser.text,得分为0,六个月内的评价数;


如何实现比较便捷,求高人详细指点啊!越详细越好..
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,