刚刚接触SQL,所以问题还真不少,希望大家多多关照问题:一个BBS的论坛,想找到各个版块中点击最高的帖子号,使用group by, max, having数据:
TID   TSID   TClickCount
1     5      2000
2     5      1800
3     5      2100
4     3      1200
5     3      1500
6     2      1000返回结果:
帖子号   版块号   最高点击数
3        5        2100
5        3        1500
6        2        1000我的试验,可以把版块的最高点击找出来,但是死活搞不出帖子号(因为group by 有限制),郁闷,希望高手能指点, 谢谢

解决方案 »

  1.   

    SELECT * FROM TB T WEHRE NOT EXISTS(SELECT 1 FROM TB WHERE TSID=T.TSID AND TClickCount>T.TClickCount)
      

  2.   

    SELECT 
        B.*
    FROM (
        SELECT 
            TSID,
            MAX(TclickCount) AS 最高点击数
        FROM BBS
        GROUP BY TSID
    ) AS A 
        JOIN BBS AS B
            ON A.TSID=B.TSID,
                 AND A.最高点击数=B.TclickCount
      

  3.   

    select * 
    from tb t
    where not exists(select * from tb where tsid=t.tsid and TClickCount>t.TClickCount)
      

  4.   

    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([TID] int,[TSID] int,[TClickCount] int)
    insert [tb]
    select 1,5,2000 union all
    select 2,5,1800 union all
    select 3,5,2100 union all
    select 4,3,1200 union all
    select 5,3,1500 union all
    select 6,2,1000
     
    ---查询---
    select * 
    from tb t
    where not exists(select * from tb where tsid=t.tsid and TClickCount>t.TClickCount)---结果---
    TID         TSID        TClickCount 
    ----------- ----------- ----------- 
    3           5           2100
    5           3           1500
    6           2           1000(所影响的行数为 3 行)
      

  5.   

    我不用 group byWith RankByClick as
    (select tid, tsid, tclickcount,
    rk = rank() over (partition by tsid order by tclickcount desc)
    from test )
    select tid, tsid, tclickcount from RankByClick where rk = 1
      

  6.   

    原来有比问题更郁闷的事: 看了答案也看不懂, 哈哈
    哎, 不知道各位大侠是否可以延着菜鸟写的思路,指点一下:select TID as 帖子号, TSID as 版块号, MAX(TClickCount) as 最高点击数
    from BBSTopic
    group by TID, TSID
    having 还是说这就是一个牛角尖?
      

  7.   

    这个用于解决当出现相同 tclickcount的问题With RankByClick as
    (select tid, tsid, tclickcount,
    rk = rank() over (partition by tsid order by tclickcount desc,tid asc)
    from test )
    select tid, tsid, tclickcount from RankByClick where rk = 1 
      

  8.   

    請問樓主想如何處理相同 tclickcount的情況?
      

  9.   

    谁能用
    select TID as 帖子号, TSID as 版块号, MAX(TClickCount) as 最高点击数 
    这样写出来呢?
      

  10.   


    你这样写法没办法延续下去,max是一个分组里的最大值,用TID、TSID作为分组,由于TID是唯一的,所以得到的结果还是它本身可以这样(先把每个版块号的最高点击数查出来,然后再从原来的表中找到匹配的板块号和点击数)
    select a.* 
    from BBSTopic a,
    (select TSID, MAX(TClickCount) as TClickCount from BBSTopic group by  TSID)b
    where a.TSID=b.TSID and a.TClickCount=b.TClickCount
    /**
    TID         TSID        TClickCount 
    ----------- ----------- ----------- 
    3           5           2100
    5           3           1500
    6           2           1000
    **/
      

  11.   


    我的答案倒不难理解吧?经已解决了楼主的问题呢,又能随你意愿控制如何处理相同 tclickcount,强行使用 group by的话,我反而想不到呢   哈哈。楼上的问案也需要依楼主的需求【相同的随便取一个就行】,作出点改动,请指教
      

  12.   


      declare @数据 table (tid int,tsid int,tclickcount int)
    insert into @数据 select 1,5,2000
           union all  select 2,5,1800
           union all  select 3,5,2100
           union all  select 4,3,1200
           union all  select 5,3,1500
           union all  select 6,2,1000
     select * from @数据 a where a.tclickcount= (select MAX(tclickcount) from @数据 where tsid=a.tsid)
    3 5 2100
    5 3 1500
    6 2 1000
      
      

  13.   

    谢谢各位的帮助, 由于个人的理解能力有限, 相对来说从josy的答案比较适合我这个菜鸟, 再次感谢大家