现有一个视频表
数据如下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),麻烦大家了!!!!

解决方案 »

  1.   


    --> 测试数据:[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]
      

  2.   

    select 
    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
      

  3.   

    declare @a table(V_id INT,  TP_ID INT, ClickCount INT,  CommentCount INT,  PublishTime varchar(20))
    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 行)
    */