select top 1 distinct a.userid,b.description,count(*) 
from table1 a,table2 b 
where a.userid = b.userid and a.description ='进入系统'  
group by a.userid,b.description 
order by a.userid,b.description desc

解决方案 »

  1.   

    你的记录数指什么意思?
    Is it right?select distinct a.userid,b.description,count(*) 
    from table1 a,table2 b 
    where a.userid = b.userid and a.description ='进入系统'  
    group by a.userid,b.description 
    having a.userid=2
      

  2.   

    select top 1 a.userid,b.description,count(*) 
    from table1 a,table2 b 
    where a.userid = b.userid and a.description ='进入系统'  
    group by a.userid,b.description 
    order by a.userid,b.description desc
      

  3.   

    sorryselect distinct a.userid,b.description,count(*) 
    from table1 a,table2 b 
    where a.userid = b.userid and a.description ='进入系统' and a.userid=2  
    group by a.userid,b.description
      

  4.   

    select count(*) from
    (
    select distinct a.userid,b.description,count(*) 
    from table1 a,table2 b 
    where a.userid = b.userid and a.description ='进入系统'  
    group by a.userid,b.description 
    order by a.userid,b.description 
    ) k
      

  5.   

    其实就是将上面的查询结果看作一个表,然后统计该表中的记录,只不过我想用一个SQL语句实现。请各位指教。
      

  6.   

    Taken(铁拳):
     我在查询分析器下按你的方法执行却提示如下: 
                  服务器: 消息 1033,级别 15,状态 1,行 8
    除非同时指定了 TOP,否则 ORDER BY 子句在视图、内嵌函数、派生表和子查询中无效。
      

  7.   

    对不起,我疏忽了,的确是这样的,需要把order by的内容去掉就可以了反正你统计条数,order by对你没什么用的如果一个记录集想作为视图、内嵌函数等内容的时候是不能有order by的,如果你创建视图的时候里面有order by 系统一样会报错的
      

  8.   

    Taken(铁拳):你好!
       我想知道你回复的k代表什么?   当我将order by子句去掉后依然报错:第 7 行: ')' 附近有语法错误。
    select count(*) from
    (
    select distinct a.userid,b.description,count(*) 
    from table1 a,table2 b 
    where a.userid = b.userid and a.description ='进入系统'  
    group by a.userid,b.description 

      

  9.   

    Taken(铁拳):你好!
    我实在看不出有什么错,是否select count(*)也不能用在嵌套的语句中?
      

  10.   

    最后的那个k不能省略
    我没有你的数据库,不能测试
    如果需要你可以把你的数据库给我发过来
    [email protected]
    附件不要大于1M
      

  11.   

    Taken(铁拳):你好!
    我的真实SQL语句如下:select distinct a.yhh,b.description,count(*) 
    from r_yhrz a,p_user b 
    where a.yhh = b.id and a.description ='进入系统'  
    group by a.yhh,b.description 
    order by a.yhh,b.description我的目的还是想用一句得到上面结果的记录数在我使用了你的方法后提示:服务器: 消息 8155,级别 16,状态 2,行 1
    没有为第 3 列(属于 'k')指定列。
    对应SQL:
    select count(*) from
    (
    select distinct a.yhh,b.description,count(*) 
    from r_yhrz a, p_user  b 
    where a.yhh = b.id and b.description ='进入系统'  
    group by a.yhh,b.description 
    )  k
      

  12.   

    Taken(铁拳):你好!
       不好意思,上上楼的错误已经调试,SQL如下:
    select count(yhh) as k from
    (
    select distinct a.yhh,b.description,count(*) as g
    from r_yhrz a, p_user  b 
    where a.yhh = b.id and b.description ='进入系统'  
    group by a.yhh,b.description 
    ) k结果:k
         
          0
    问题是:就象我每一次发贴时提到的,应该有两条记录才对
      

  13.   

    不知道你的SQL是什么版本的,我用的SQL7,恢复数据库时报错,最好导成access格式的再发过来
      

  14.   

    Taken(铁拳):你好!
     不好意思,上上楼的写错了,问题已经解决。
     我还想知道下面的SQL为什么错:
    select top 1 distinct a.yhh,b.description,count(*) 
    from r_yhrz a,p_user b 
    where a.yhh = b.id and a.description ='进入系统'  
    group by a.yhh,b.description 
    order by a.yhh,b.description   我想得到结果中第一条记录
      

  15.   

    我想昨天 tj_dns(愉快的登山者)的回复有问题:
      正确的SQL就为:
    select top 1 * from
    (
    select distinct a.yhh,b.description,count(*) as num
    from r_yhrz a, p_user  b 
    where a.yhh = b.id and a.description ='进入系统'  
    group by a.yhh,b.description 
    )
    k
    问题全部解决。
    再次感谢Taken(铁拳)参与