【数据库为2000】字段:id,fromplace,toplace,terrace,year,month,scount
说明:
fromplace:出发地
toplace:目的地
terrace:平台
scount:搜索次数
logyear:记录年
logmonth:记录月根据“出发地--目的地”按搜索次数统计每个月前10名,
由于有不同平台的数据,所以需要把“出发地--目的地”一样的先按月统计好搜索次数,再根据搜索次数的多少作统计。测试数据:上海 北京 com 2009 08 55
北京 大连 com 2009 08 44
天津 北京 com 2009 08 33
沈阳 大连 com 2009 08 22
北京 石家庄 com 2009 08 11北京 天津 net 2009 08 55
上海 杭州 net 2009 08 44
北京 成都 net 2009 08 33
上海 北京 net 2009 08 22
北京 杭州 net 2009 08 11杭州 上海 com 2009 09 55
上海 苏州 com 2009 09 44
北京 石家庄 com 2009 09 33
北京 大连 com 2009 09 22
广州 深圳 com 2009 09 11上海 杭州 net 2009 09 55
杭州 上海 net 2009 09 44
北京 上海 net 2009 09 33
北京 天津 net 2009 09 22
北京 杭州 net 2009 09 11

解决方案 »

  1.   

    ;with f as
    (
     select
      fromplace,toplace,terrace,year,month,sum(scount) as scount 
     from
      tb
     group by
      fromplace,toplace,terrace,year,month
    )
    select
     * 
    from
     tb t 
    where 
     scount 
    in(select top 10 scount from tb where fromplace=t.fromplace and toplace=t.toplace order by scount desc)
      

  2.   

    ;with f as
    (
     select
      fromplace,toplace,terrace,year,month,sum(scount) as scount 
     from
      tb
     group by
      fromplace,toplace,terrace,year,month
    )
    select
     * 
    from
     f t 
    where 
     scount 
    in(select top 10 scount from f where fromplace=t.fromplace and toplace=t.toplace order by scount desc)
      

  3.   


    已经注明了是【SQL2000】是组合后的每月前10条
      

  4.   


    从现在起,我要努力学习SQL,不然稍微复杂点SQL语句就不会(即使会,但是效率不是那么高)
      

  5.   

    --sql2000没有row_number函数 及不支持cte,用下面语句
    --tablename 是统计后scount的表
    select * from 
    (select *,
    rownum=(select count(1) from tablename where terrace=t.terraceand logyear=t.logyear and logmonth=t.logmonth and scount>=t.scount)
    from tablename t
    ) tt
    where rownum<=10  --这里指定第几到第几