有问题贴到CSDN吧,这里有很多热心的人.我是混分的~~

解决方案 »

  1.   

    这是表tb
    name    val     memo
    a 2 a
    a 1 b
    a 3 c
    b 1 d
    b 3 e
    b 2 f
    b 4 g
    b 5 h要查出name分组取最小的两个(N个)val
    这是正确的代码select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
    但是就是不懂,很不懂:(
      

  2.   

    --按某一字段分组取最大(小)值所在行的数据(2007-10-23于浙江杭州)
    /*
    数据如下:
    name val memo
    a    2   a2(a的第二个值)
    a    1   a1--a的第一个值
    a    3   a3:a的第三个值
    b    1   b1--b的第一个值
    b    3   b3:b的第三个值
    b    2   b2b2b2b2
    b    4   b4b4
    b    5   b5b5b5b5b5
    */
    --创建表并插入数据:
    create table tb(name varchar(10),val int,memo varchar(20))
    insert into tb values('a',    2,   'a2(a的第二个值)')
    insert into tb values('a',    1,   'a1--a的第一个值')
    insert into tb values('a',    3,   'a3:a的第三个值')
    insert into tb values('b',    1,   'b1--b的第一个值')
    insert into tb values('b',    3,   'b3:b的第三个值')
    insert into tb values('b',    2,   'b2b2b2b2')
    insert into tb values('b',    4,   'b4b4')
    insert into tb values('b',    5,   'b5b5b5b5b5')
    go--一、按name分组取val最大的值所在行的数据。
    --方法1:
    select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
    --方法2:
    select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
    --方法3:
    select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
    --方法4:
    select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
    --方法5
    select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          3           a3:a的第三个值
    b          5           b5b5b5b5b5
    */--二、按name分组取val最小的值所在行的数据。
    --方法1:
    select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
    --方法2:
    select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
    --方法3:
    select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
    --方法4:
    select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
    --方法5
    select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          1           a1--a的第一个值
    b          1           b1--b的第一个值
    */--三、按name分组取第一次出现的行所在的数据。
    select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          2           a2(a的第二个值)
    b          1           b1--b的第一个值
    */--四、按name分组随机取一条数据。
    select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          1           a1--a的第一个值
    b          5           b5b5b5b5b5
    */--五、按name分组取最小的两个(N个)val
    select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
    select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
    select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          1           a1--a的第一个值
    a          2           a2(a的第二个值)
    b          1           b1--b的第一个值
    b          2           b2b2b2b2
    */--六、按name分组取最大的两个(N个)val
    select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
    select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
    select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          2           a2(a的第二个值)
    a          3           a3:a的第三个值
    b          4           b4b4
    b          5           b5b5b5b5b5
    */
      

  3.   

    就用这个例子说一下sql语句的理解方法:
        select   a.*   from   tb   a   where   2   >   (select   count(*)   from   tb   where   name   =   a.name   and   val   >   a.val   )   order   by   a.name,a.val 我们看SQL的语法:select [字段] 
    from [table] 
    group by [分组]
    where [条件] 
    order by [顺序]


    语句中没有group by, 所以语句形式变为:select [字段] 
    from [table] 
    where [条件] 
    order by [顺序]

    1. 现在我们来分层看上面的语句: select   a.*   
    from   tb   a   
    where   2   >   (select   count(*)   from   tb   where   name   =   a.name   and   val   >   a.val   )   
    order   by   a.name,a.val 

    2. order by 语句只起顺序作用,对选择逻辑无影响,删除它,变成。
    select   a.*   
    from   tb   a   
    where   2   >   (select   count(*)   from   tb   where   name   =   a.name   and   val   >   a.val   ) 

    3. 然后select a.* 和 from tb a 都是简单表达式,也可以不予理会(好容易理解嘛),现在重点来理解 where后面的子句: 2   >   (select   count(*)   from   tb   where   name   =   a.name   and   val   >   a.val   )4. 子句中有一个select子句,我们按上面的方法来解开这个select子句: select count(*)
    from tb
    where name =a.name and val > a.val

    5. 这个语句的意思明白了:就是找出表tb中name = a.name 和 val>a.val的记录数(a为主表的别名,其实就是本身),也就是说找出名称相当,val比较大的记录数。6. 联合起来:
    2   >   (select   count(*)   from   tb   where   name   =   a.name   and   val   >   a.val   )
    就是val最小的两条记录。7. 这样:where后面的条件就解决了。于是原来的语句也就明白了,将原来的sql简化如下: select   a.*   
    from   tb   a   
    where   [val最小的两条记录]   
    order   by   a.name,a.val 

    这样就明白了吧。bingo!