现有一个视频表
数据如下V_id TP_ID ClickCount CommentCount PublishTime
1 1 10 10 2010-01-01
2 1 20 20 2010-01-02
3 1 30 30 2010-01-03
4 1 40 40 2010-01-04
5 2 50 20 2010-01-05
6 2 60 20 2010-01-06
7 2 70 20 2010-01-07
8 2 80 20 2010-01-08
9 2 90 10 2010-01-09V_id是视频的主键,
TP_ID是视频的类别,
clickCount是视频的点击次数,
CommentCount是视频的评论次数,
publishTIme是视频的发布时间
现在想要如下统计结果V_id TP_ID totalClickCount totalCommentCount PublishTime
9 2 350 90 2010-01-09
4 1 100 100 2010-01-04就是根据每种类别下的 所有视频的点击次数总和降序排列
所得的字段分别为:
视频ID(V_ID),
视频类别(TP_ID),
该类别下的所有视频点击次数和(totalClickCount)
该类别下的所有视频总评论次数(totalCommentCount )
该类别下发布时间最晚的那个视频的发布时间(publishTime),麻烦大家了!!!!
数据如下V_id TP_ID ClickCount CommentCount PublishTime
1 1 10 10 2010-01-01
2 1 20 20 2010-01-02
3 1 30 30 2010-01-03
4 1 40 40 2010-01-04
5 2 50 20 2010-01-05
6 2 60 20 2010-01-06
7 2 70 20 2010-01-07
8 2 80 20 2010-01-08
9 2 90 10 2010-01-09V_id是视频的主键,
TP_ID是视频的类别,
clickCount是视频的点击次数,
CommentCount是视频的评论次数,
publishTIme是视频的发布时间
现在想要如下统计结果V_id TP_ID totalClickCount totalCommentCount PublishTime
9 2 350 90 2010-01-09
4 1 100 100 2010-01-04就是根据每种类别下的 所有视频的点击次数总和降序排列
所得的字段分别为:
视频ID(V_ID),
视频类别(TP_ID),
该类别下的所有视频点击次数和(totalClickCount)
该类别下的所有视频总评论次数(totalCommentCount )
该类别下发布时间最晚的那个视频的发布时间(publishTime),麻烦大家了!!!!
解决方案 »
- 跪求大神赐一个sql语句···
- 请大侠们帮我看下我的存储过程
- 用了identify标识的字段是如何工作的?
- 今天安装测试工具QC,安装的时候连接数据库时候老是连接不上,我的数据库是SQL server 2005的,同学们都连接起了他们是SQL server 2000
- 这种存储过程如何实现(挑战高手)
- 招聘软件工程师
- 如何用一条SQL语句,将C:\mydata\book1.txt中的所有数据传入tabe1表的bbb字
- Access更新索引,头痛了
- ASP.NET的消息框问题???,我已经无分了,体谅一下吧!
- 关于一个留言簿表结构的问题
- SQL Server 表添加新列出现不能保存问题
- 删除了 mssql的data目录下mdf和ldf文件,重新创建,怎么提示Database 'test' already exists.
--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
create table [TB]([V_id] int,[TP_ID] int,[ClickCount] int,[CommentCount] int,[PublishTime] datetime)
insert [TB]
select 1,1,10,10,'2010-01-01' union all
select 2,1,20,20,'2010-01-02' union all
select 3,1,30,30,'2010-01-03' union all
select 4,1,40,40,'2010-01-04' union all
select 5,2,50,20,'2010-01-05' union all
select 6,2,60,20,'2010-01-06' union all
select 7,2,70,20,'2010-01-07' union all
select 8,2,80,20,'2010-01-08' union all
select 9,2,90,10,'2010-01-09'select V_id=max([V_id]),
TP_ID,
totalClickCount=sum(ClickCount),
totalCommentCount=sum(CommentCount),
PublishTime=max([PublishTime])
from [TB]
group by TP_ID
order by V_id desc/*
V_id TP_ID totalClickCount totalCommentCount PublishTime
----------- ----------- --------------- ----------------- -----------------------
9 2 350 90 2010-01-09 00:00:00.000
4 1 100 100 2010-01-04 00:00:00.000(2 行受影响)*/drop table [TB]
max(V_ID) V_ID,
TP_ID,
sum(ClickCount) totalClickCount ,
sum(CommentCount) totalCommentCount ,
max(PublishTime) PublishTime
from tb
groupp by TP_ID
order by sum(ClickCount) desc
insert @a select 1,1,10,10,'2010-01-01'
union all select 2,1,20,20,'2010-01-02'
union all select 3,1,30,30,'2010-01-03'
union all select 4,1,40,40,'2010-01-04'
union all select 5,2,50,20,'2010-01-05'
union all select 6,2,60,20,'2010-01-06'
union all select 7,2,70,20,'2010-01-07'
union all select 8,2,80,20,'2010-01-08'
union all select 9,2,90,10,'2010-01-09'SELECT
MAX(V_id) V_Id,
TP_Id,
SUM(ClickCount) totalClickCount,
sum(CommentCount) totalCommentCount,
MAX(PublishTime) PublishTime
FROM @a
GROUP BY TP_ID
ORDER BY 2 DESC
--result
/*
V_Id TP_Id totalClickCount totalCommentCount PublishTime
----------- ----------- --------------- ----------------- --------------------
9 2 350 90 2010-01-09
4 1 100 100 2010-01-04(所影响的行数为 2 行)
*/