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

如何统计字段值的个数?

各位好!

有张表的字段分别为:user,time,score
user是用户名,time是分数产生时间,score是三种分数:1,-1,0

需要统计:

距今3个月内得分为1的用户数,距今3个月内得分为-1的用户数,距今3个月内得分为0的用户数

请问用SQL语句如何统计呢?

若能给各例子出来就更感谢了!
(类似统计考试分为60以下,60-70,71-80,81-90,91-100的学生数也行) --------------------编程问答-------------------- select count(user) from #Table where (time between DATEADD(MONTH,-3,GETDATE()) AND GETDATE() ) AND score=1 --------------------编程问答-------------------- 上面的即可 --------------------编程问答-------------------- --------------------编程问答-------------------- SELECT SUM(id) CASE scoure when score=1 then '得分为1的用户'
                            when score=-1 then '得分为-1的用户'   
                            when score=0 then '得分为0的用户'  
试一下,好像是这样,以前做过 --------------------编程问答--------------------


select count(user),score 
from #Table 
where (time between DATEADD(MONTH,-3,GETDATE()) AND GETDATE() ) 
group by score 

--------------------编程问答--------------------
SELECT SUM(id) CASE scoure 
when score=1  and time between DATEADD(MONTH,-3,GETDAT())AND GETDATE() then '得分为1的用户' 
 when score=-1 and time between DATEADD(MONTH,-3,GETDAT())AND GETDATE() then '得分为-1的用户'  
 when score=0  and time between DATEADD(MONTH,-3,GETDAT())AND GETDATE()then '得分为0的用户' 
else
 null end
刚刚忘记取时间了 不好意思哈楼主 --------------------编程问答--------------------



DECLARE @TABLE TABLE
(
USERID INT,
RETIME DATETIME,
SCORE INT
)

INSERT INTO @TABLE VALUES(1,'2009-09-01',-1)
INSERT INTO @TABLE VALUES(2,'2009-09-04',-1)
INSERT INTO @TABLE VALUES(3,'2009-11-01',-1)
INSERT INTO @TABLE VALUES(1,'2009-09-01',0)
INSERT INTO @TABLE VALUES(2,'2009-09-04',0)
INSERT INTO @TABLE VALUES(3,'2009-11-01',0)
INSERT INTO @TABLE VALUES(1,'2009-09-01',1)
INSERT INTO @TABLE VALUES(2,'2009-09-04',1)
INSERT INTO @TABLE VALUES(3,'2009-11-01',1)
INSERT INTO @TABLE VALUES(1,'2009-07-01',0)
INSERT INTO @TABLE VALUES(2,'2009-09-04',0)
INSERT INTO @TABLE VALUES(3,'2009-06-01',0)


select count(USERID)AS USERCNT,SCORE 
from @TABLE 
where (RETIME between DATEADD(MONTH,-3,GETDATE()) AND GETDATE() ) 
group by SCORE 

--------------------编程问答-------------------- 2 -1
3 0
2 1
--------------------编程问答-------------------- 说了这么多
还不给分啊? --------------------编程问答-------------------- d --------------------编程问答-------------------- count --------------------编程问答--------------------
select count(user) from #Table where (time between DATEADD(MONTH,-3,GETDATE()) AND GETDATE() ) AND score=1
这个应该可以 --------------------编程问答-------------------- 5楼正解了
分组查询
楼主的结贴率让各位担忧 --------------------编程问答-------------------- 看了这些,还不知道在.cs页面,如何将查询到的结果赋给变量啊。
比如定义了得分为1的变量是haopin,得分为0的变量是zhongpin,得分为-1的变量是chapin.
如何通过查询统计出得分为1的用户数,然后赋给变量haopin呢?

有些查询的变量时这么赋值的:
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
            string sql = "select * from DianPuDetail where DianPuId=" + dianpuid;
            SqlCommand command = new SqlCommand(sql, conn);
            conn.Open();
            SqlDataReader dr = command.ExecuteReader();
            if (dr.Read())
            {
                shopad = dr["ShopAD"].ToString();
                dianpuname = dr["DianPuName"].ToString();
                dianpuuser = dr["DianPuUser"].ToString();
                Lbluser.Text = dianpuuser;}
这种该如何写啊?!

麻烦各位再指点迷津,OK了肯定给分!谢谢了! --------------------编程问答-------------------- 在CS页面如何统计并显示出来啊!
没人回答了吗? --------------------编程问答-------------------- 可能之前说得有点简单,用各位的方法不知如何实现,再详细说明:


数据表名: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
部份技术文章来自网络,