有很多记录 怎样统计最后一条记录 在所有的记录中 “连续” 出现最多的次数
假如 字段 id ,第一列(varchar),第二列(varchar)。
id ,第一列,第二列。
1    3       4
2    3       5
3    3       6
4    4       6
5    5       7
6    3       6
7    6       5
8    2       5
9    3       6第九条数据记录 第一列的是 “3”  第二列的 是“6”
我想的到的结果是
第一列次数   第二列次数
  3             2就是最后一条数据在表中在所有数据中“连续”出现最多的次数

解决方案 »

  1.   

    create table test(id int identity,col1 varchar(2),col2 varchar(2))
    go
    insert test select  '3','4'
    union all select    '3','5'
    union all select    '3','6'
    union all select    '4','6'
    union all select    '5','7'
    union all select    '3','6'
    union all select    '6','5'
    union all select    '2','5'
    union all select    '3','6'
    goselect 
      (select count(distinct t1.id) 
         from test t1,test t2 
        where (t2.id = t1.id +1 or t1.id = t2.id + 1) and 
               t1.col1 = s.col1 and t2.col1 = s.col1) as 第一列次数,
      (select count(distinct t1.id) 
         from test t1,test t2 
        where (t2.id = t1.id +1 or t1.id = t2.id + 1)  and 
               t1.col2 = s.col2 and t2.col2 = s.col2) as 第二列次数
    from 
      (select col1,col2 from test where id = (select max(id) from test)) s--结果
    第一列次数 第二列次数
      3           2
      

  2.   

    to ydage(Ljr) 哥们 你这个代码 是写出来的 还是用sql生成一部分,如果是部分生成的话,是怎么生成的,能告之吗 ,谢谢了
      

  3.   

    To 2004v2004:
    --------------------
    让你见笑了,应该说是试出来的才对(还不是很完善),主要用的就是相关子查询(没有特别深的知识)
      

  4.   

    你的语句在 access下不好使  ,在sql下面没有问题,能帮忙下 看怎么改能在access下能使用
      

  5.   

    在access里边 不能这样用select count(distinct t1.id) 
    这样即可select count( t1.id) 
      

  6.   

    to 2004v2004:
    --------------
    非常抱歉,ACCESS很少使用,所以就不能帮助你做转换了