日期:2014-05-18  浏览次数:20389 次

【散50分求一SQL语句】【单表聚合分组查询】【闪电结贴】
新闻表:

      表名:   news

      字段:   id           新闻编号
                    title     新闻标题
                    type       新闻分类
                    click     该文章点击次数
                    ...


        id         title       type     click     ...
      -----   -------   ------   ------
      00012       标题1       1001       10       ...
      00013       标题2       1002       58      
      00014       标题3       1001       21      
      ...

现在需要按 <新闻分类> 进行分类,求出该分类的文章数量和该分类下所有的文章点击总和     如:

    type     number   clickall  
  ------   ------   ---------
      1001       15         102  
      1002       1           91
      1003       6           520

number     为该分类下文章的数量
clickall     就是该分类下面所属文章点击次数的和


先谢谢进来的各位了:-)

------解决方案--------------------
select type , count(*) number , sum(click) click from news group by type
------解决方案--------------------
select type,count(*) as number,sum(click) as clickall from news GROUP BY type
------解决方案--------------------
select type , count(*) as number , sum(click) as click from news group by type
这样更准确吧!不过楼上的应该也可以的
------解决方案--------------------
create table news(id varchar(10),title varchar(10),type int,click int)
insert into news values( '00012 ', '标题1 ', 1001, 10)
insert into news values( '00013 ', '标题2 ', 1002, 58)
insert into news values( '00014 ', '标题3 ', 1001, 21)
select type , count(*) number , sum(click) click from news group by type
drop table news

/*
type number click
----------- ----------- -----------
1001 2 31
1002 1 58

(所影响的行数为 2 行)
*/
------解决方案--------------------
楼上的太复杂了吧!不过也实现功能了
------解决方案--------------------
指我的复杂?看清楚了再说话.
------解决方案--------------------
- -! 你ls是在建表