一张表,内容如下:所有项目均可重复
出现次数   A   B  C  D  E  id
     1   …… ……  ……   001
     1   …… ……  ……   002
     1   …… ……  ……   003
     2   …… ……  ……   001
     3   …… ……  ……   001
     2   …… ……  ……   002
     2   …… ……  ……   003
     1   …… ……  ……   004
     3   …… ……  ……   002
     3   …… ……  ……   003
     4   …… ……  ……   001
……
现在要实现的是:当查询条件是出现次数时,怎么查询出所有大于等于出现次数为2的项,并且结果为次数最大的那项。
比如当查询条件为 次数是2时,id为001的这项会出现3次,而现在需要只将最大的那项显示出来,即是次数为4的那项,其他也一样。我用了 IN, 用了 group by ,怎么组织都实现不出来哎
         等高人指点

解决方案 »

  1.   

    select t.* from tb t where 出现次数 >= 2 and 出现次数 = (select max(出现次数) from tb where 出现次数 >= 2 and id = t.id) order by t.idselect t.* from tb t where 出现次数 >= 2 and not exists (select max(出现次数) from tb where 出现次数 >= 2 and id = t.id and 出现次数 > t.出现次数) order by t.id
      

  2.   

    select 
     * 
    from
     tb t 
    where 
     出现次数 = (select max(出现次数) from tb where 出现次数 >= 2 and id = t.id) 
    and 
     出现次数 >= 2  
    order by
     t.id 
      

  3.   

    create table tb(出现次数 int , id varchar(10))
    insert into tb values(1   ,'001') 
    insert into tb values(1   ,'002') 
    insert into tb values(1   ,'003') 
    insert into tb values(2   ,'001') 
    insert into tb values(3   ,'001') 
    insert into tb values(2   ,'002') 
    insert into tb values(2   ,'003') 
    insert into tb values(1   ,'004') 
    insert into tb values(3   ,'002') 
    insert into tb values(3   ,'003') 
    insert into tb values(4   ,'001')
    goselect t.* from tb t where 出现次数 >= 2 and 出现次数 = (select max(出现次数) from tb where 出现次数 >= 2 and id = t.id) order by t.idselect t.* from tb t where 出现次数 >= 2 and not exists (select 1 from tb where 出现次数 >= 2 and id = t.id and 出现次数 > t.出现次数) order by t.iddrop table tb /*
    出现次数        id         
    ----------- ---------- 
    4           001
    3           002
    3           003(所影响的行数为 3 行)出现次数        id         
    ----------- ---------- 
    4           001
    3           002
    3           003(所影响的行数为 3 行)
    */
      

  4.   

    --出现次数可以用变量代入.如下:
    create table tb(出现次数 int , id varchar(10))
    insert into tb values(1   ,'001') 
    insert into tb values(1   ,'002') 
    insert into tb values(1   ,'003') 
    insert into tb values(2   ,'001') 
    insert into tb values(3   ,'001') 
    insert into tb values(2   ,'002') 
    insert into tb values(2   ,'003') 
    insert into tb values(1   ,'004') 
    insert into tb values(3   ,'002') 
    insert into tb values(3   ,'003') 
    insert into tb values(4   ,'001')
    godeclare @出现次数 as intset @出现次数 = 2select t.* from tb t where 出现次数 >= @出现次数 and 出现次数 = (select max(出现次数) from tb where 出现次数 >= @出现次数 and id = t.id) order by t.idselect t.* from tb t where 出现次数 >= @出现次数 and not exists (select 1 from tb where 出现次数 >= @出现次数 and id = t.id and 出现次数 > t.出现次数) order by t.id/*
    出现次数        id         
    ----------- ---------- 
    4           001
    3           002
    3           003(所影响的行数为 3 行)出现次数        id         
    ----------- ---------- 
    4           001
    3           002
    3           003(所影响的行数为 3 行)
    */set @出现次数 = 4select t.* from tb t where 出现次数 >= @出现次数 and 出现次数 = (select max(出现次数) from tb where 出现次数 >= @出现次数 and id = t.id) order by t.idselect t.* from tb t where 出现次数 >= @出现次数 and not exists (select 1 from tb where 出现次数 >= @出现次数 and id = t.id and 出现次数 > t.出现次数) order by t.id
    /*出现次数        id         
    ----------- ---------- 
    4           001(所影响的行数为 1 行)出现次数        id         
    ----------- ---------- 
    4           001(所影响的行数为 1 行)*/drop table tb 
      

  5.   

    感谢两位这么快速和详细的回答
      但有一个问题,就是我用的数据库不是SQL Server, 而是Access ,所以有点不明白查询时 select t.* from tb t 是什么意思这个表 t 不知道代表什么还没怎么深入SQL Server 这是我的查询原句:
    SELECT * FROM PasteOrderTb WHERE RecordNum = (SELECT MAX(RecordNum) FROM PasteOrderTb WHERE RecordNum >= "&RecordNum&") AND RecordNum >= "&RecordNum&" ORDER BY AutoID DESC
    是在ASP程序里运行的,但结果只有一条记录,只有次数是最大的那项。
      

  6.   

    建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  7.   


    IF OBJECT_ID('[TB]') IS NOT NULL DROP TABLE TBcreate table tb(出现次数 int , id varchar(10))
    insert into tb values(1   ,'001') 
    insert into tb values(1   ,'002') 
    insert into tb values(1   ,'003') 
    insert into tb values(2   ,'001') 
    insert into tb values(3   ,'001') 
    insert into tb values(2   ,'002') 
    insert into tb values(2   ,'003') 
    insert into tb values(1   ,'004') 
    insert into tb values(3   ,'002') 
    insert into tb values(3   ,'003') 
    insert into tb values(4   ,'001')
    SELECT MAX(出现次数) AS 出现次数 ,ID FROM TB WHERE 出现次数>=2 GROUP BY [ID]出现次数        ID         
    ----------- ---------- 
    4           001
    3           002
    3           003(所影响的行数为 3 行)SELECT TOP 1 *  FROM (SELECT MAX(出现次数) AS 出现次数 ,ID FROM TB WHERE 出现次数>=2 GROUP BY [ID] ) B ORDER BY 出现次数 DESC
    出现次数        ID         
    ----------- ---------- 
    4           001(所影响的行数为 1 行)
      

  8.   

    表的创建都直接在Access里鼠标点点操作完成的,表的结构基本就是我在问题描述里描述的那样,
    一张表,内容如下:所有项目均可重复 
    出现次数  A  B  C  D  E  id 
        1  …… ……  ……  001 
        1  …… ……  ……  002 
        1  …… ……  ……  003 
        2  …… ……  ……  001 
        3  …… ……  ……  001 
        2  …… ……  ……  002 
        2  …… ……  ……  003 
        1  …… ……  ……  004 
        3  …… ……  ……  002 
        3  …… ……  ……  003 
        4  …… ……  ……  001 
    …… 
    而且这是一张已经存在的表,不需要再creat,只要把符合条件的结果查询出来就行。
      

  9.   

    那些create , insert是我们的测试数据.
    你需要看后面的查询语句即可.
      

  10.   

    我知道只需要看查询语句就可以的呵呵,MS Sql还是上学的时候弄过一阵子,学了点基础,后来工作后因为工作需要,跟好几种数据库打过交道,但就是没MS Sql 现在一直在捣腾Access,前面几位高人说的方法,对Access好像行不通,因为都用到了一个 t 虚拟表,也没得到结果,不过还是非常感谢大家的帮忙
      

  11.   

    TO  bestt8:      你的方法我试了下,如果只是查询2个项目,即是 “SELECT MAX(出现次数) AS 出现次数 ,ID……”  那是可以得到结果的,但我的这个表里可不止2个项目十几个项目,好像也不大行得过不知道还要是怎么调整才能把所有的查询项目都显示出来
      

  12.   

    建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  13.   

    表结构:
    record   int
    A        Char(50)
    B        Char(50)
    C        Char(50)
    D        Char(50)
    E        Char(50)
    id       Char(50)
    测试数据:
    record  A  B  C  D  E  id 
        1   a  b  c  d  e  001 
        1   a  b  c  d  e  002 
        1   a  b  c  d  e  003 
        2   a  b  c  d  e  001 
        3   a  b  c  d  e  001 
        2   a  b  c  d  e  002 
        2   a  b  c  d  e  003 
        1   a  b  c  d  e  004 
        3   a  b  c  d  e  002 
        3   a  b  c  d  e  003 
        4   a  b  c  d  e  001 
    insert:
    asp程序: inset into Tb(record,A,B,C,D,E,id) values("&rec&",'a','b','c','d','e','"&id&"')
    select:
    asp程序: select * from Tb where record=2
    结果:
    2   a  b  c  d  e  001
    2   a  b  c  d  e  002 
    2   a  b  c  d  e  003
      

  14.   

    数据名名称: Access
    版本:  2003
      

  15.   

    1> select * from buker19999;
    2> go
    record     |a|b|c|d|e|id
    -----------|-|-|-|-|-|---
              1|a|b|c|d|e|001
              1|a|b|c|d|e|002
              1|a|b|c|d|e|003
              2|a|b|c|d|e|001
              3|a|b|c|d|e|001
              2|a|b|c|d|e|002
              2|a|b|c|d|e|003
              1|a|b|c|d|e|004
              3|a|b|c|d|e|002
              3|a|b|c|d|e|003
              4|a|b|c|d|e|001(11 rows affected)
    1> select * from buker19999 where record=2
    2> go
    record     |a|b|c|d|e|id
    -----------|-|-|-|-|-|---
              2|a|b|c|d|e|001
              2|a|b|c|d|e|002
              2|a|b|c|d|e|003(3 rows affected)
    1>你不是自己已经有语句了吗? 你想要的结果是什么样?
     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  16.   

    create table test1 

    rec  int, 
    A    varchar(20),
    B    varchar(20),
    C    varchar(20),
    D    varchar(20),
    E    varchar(20),
    id   varchar(20),

    insert into test1 select 1,'a','b','c','d','e','001' 
    insert into test1 select 1,'a','b','c','d','e','002'
    insert into test1 select 1,'aw','bd','cf','ad','de','003' 
    insert into test1 select 2,'a','b','c','d','e','001' 
    insert into test1 select 3,'aw','db','cf','gd','he','001' 
    insert into test1 select 2,'a','b','c','d','e','002' 
    insert into test1 select 2,'ad','bw','cd','dd','de','003' 
    insert into test1 select 1,'aa','bb','cc','dd','ee','004'
    insert into test1 select 3,'ab','bc','cd','de','ef','002' 
    insert into test1 select 3,'aa','ba','ca','da','ea','003'
    insert into test1 select 4,'a','b','c','d','e','001'  
    /* 
    要得到这样的报表: 当查询条件为出现次数 rec时显示大于等于rec的最大的那条记录,比如2时
    rec A  B  C  D  E   id
     4  aa  bb  cc  dd  ee  001
     3  ab  bc  cd  de  ef  002
     3  aa  ba  ca  da  ea  003
    */ 
    drop table test1 
      

  17.   

    creat table 最后多了一个逗号。要删掉
      

  18.   

    select * from buker19999 t
    where record>2
    and not exists (
    select 1 from buker19999
    where id=t.id and record>t.record
    )楼主,不管你给不给我分,我都要坦白的说,“你的语文很烂!" 甚至可以说,你不适合做计算机软件行业。
      

  19.   

    这不是给不给分的问题,也不是语文烂不烂的问题,我觉得我描述得挺清楚的了,我也说了,我对MS SQL没有研究,我觉得高手可以根据我的描述去creat一个环境的,你说不明白,那我也继续更加详细,也照你给我的例子去做了更加详细的描述,所以,你也没必要用黑体字骂我的语文很烂,也没必要妄断我适合不适合做计算机行业吧。也许你在计算机的某一个领域很厉害,一点就通,但能说明对整个计算机行业都精通吗多给新手和某一些领域生疏的人一些耐心就好了大家都会非常感谢你的帮助的可能我最大的错误就是发错板块了。你的 select 还是用到了 t 这种虚拟表,而貌似在ASP程序+Access这样的环境中这样是行不通的。还是非常感谢你的帮忙