现求一简单sql语句,希望各位大虾能帮我一下 proId  cateId    proName      proDetail                                       proPrice 
5000    1          奥迪        德国制造的经典车型,中国就研究不出自己的品牌吗?   500000        
5001    1          丰田        日本侵略中国的造钱工具                              200000        
5009    1          红旗         曾经的国产名牌,只有国家领导人才能坐的车子            50000 
5005    2          康乃馨      非常漂亮                                                 52 
5006    2          玫瑰花      情人都知道的东西                                        66 
5007    3          爱国者      大家都来支持国货                                        500 
5008    3          佳能        又一个小日本的产品,但是质量不得不叫人佩服            2000 其中,cateId为产品类别,现在想查出每种类别的任意一项。 
比如:种类1中的奥迪,种类2中的康乃馨,种类3中的爱国者,一次性取出这些。(每种类别中取一个); 我知道这道题对大家来说挺简单的,但是我刚毕业,没经验,希望大家能伸伸手,帮帮小弟。

解决方案 »

  1.   

    select *
    from tb a
    where proid=(select top 1 proid from tb where cateid=a.cateid order by newid())
      

  2.   

    select * from 表 a where not exists (select 1 from 表 where cateId=a.cateId and proId<a.proId)
      

  3.   

    create table tb(proid int,cateid int,proname varchar(20),prodetail varchar(600),proprice int)
    insert tb select 5000  ,  1    ,      '奥迪'    ,    '德国制造的经典车型,中国就研究不出自己的品牌吗?' ,  500000         
    insert tb select 5001  ,  1    ,      '丰田'    ,   '日本侵略中国的造钱工具'                            ,  200000         
    insert tb select 5009  ,  1    ,      '红旗'    ,     '曾经的国产名牌,只有国家领导人才能坐的车子'      ,      50000  
    insert tb select 5005  ,  2    ,      '康乃馨'  ,    '非常漂亮'                                         ,        52  
    insert tb select 5006  ,  2    ,      '玫瑰花'  ,   '情人都知道的东西'                                  ,     66  
    insert tb select 5007  ,  3    ,      '爱国者'  ,    '大家都来支持国货'                                 ,       500  
    insert tb select 5008  ,  3    ,      '佳能'    ,    '又一个小日本的产品,但是质量不得不叫人佩服'       ,     2000select *
    from tb a
    where proid=(select top 1 proid from tb where cateid=a.cateid order by newid())
    drop table tb/*
    proid       cateid      proname              prodetail                                                                                                                                                                                                                                                        proprice
    ----------- ----------- -------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------
    5000        1           奥迪                   德国制造的经典车型,中国就研究不出自己的品牌吗?                                                                                                                                                                                                                                         500000
    5005        2           康乃馨                  非常漂亮                                                                                                                                                                                                                                                             52
    5008        3           佳能                   又一个小日本的产品,但是质量不得不叫人佩服                                                                                                                                                                                                                                            2000(3 行受影响)*/
      

  4.   

    create table tb(proid int,cateid int,proname varchar(20),prodetail varchar(600),proprice int)
    insert tb select 5000  ,  1    ,      '奥迪'    ,    '德国制造的经典车型,中国就研究不出自己的品牌吗?' ,  500000         
    insert tb select 5001  ,  1    ,      '丰田'    ,   '日本侵略中国的造钱工具'                            ,  200000         
    insert tb select 5009  ,  1    ,      '红旗'    ,     '曾经的国产名牌,只有国家领导人才能坐的车子'      ,      50000  
    insert tb select 5005  ,  2    ,      '康乃馨'  ,    '非常漂亮'                                         ,        52  
    insert tb select 5006  ,  2    ,      '玫瑰花'  ,   '情人都知道的东西'                                  ,     66  
    insert tb select 5007  ,  3    ,      '爱国者'  ,    '大家都来支持国货'                                 ,       500  
    insert tb select 5008  ,  3    ,      '佳能'    ,    '又一个小日本的产品,但是质量不得不叫人佩服'       ,     2000--每类两条
    select *
    from tb a
    where proid in(select top 2 proid from tb where cateid=a.cateid order by newid())
    drop table tb/*
    proid       cateid      proname              prodetail                                                                                                                                                                                                                                                        proprice
    ----------- ----------- -------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------
    5000        1           奥迪                   德国制造的经典车型,中国就研究不出自己的品牌吗?                                                                                                                                                                                                                                         500000
    5009        1           红旗                   曾经的国产名牌,只有国家领导人才能坐的车子                                                                                                                                                                                                                                            50000
    5005        2           康乃馨                  非常漂亮                                                                                                                                                                                                                                                             52
    5006        2           玫瑰花                  情人都知道的东西                                                                                                                                                                                                                                                         66
    5007        3           爱国者                  大家都来支持国货                                                                                                                                                                                                                                                         500
    5008        3           佳能                   又一个小日本的产品,但是质量不得不叫人佩服                                                                                                                                                                                                                                            2000(6 行受影响)
    */
      

  5.   

    --按某一字段分组取最大(小)值所在行的数据
    (爱新觉罗.毓华 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
    */
    --七,如果整行数据有重复,所有的列都相同。
    /*
    数据如下:
    name val memo
    a    2   a2(a的第二个值)
    a    1   a1--a的第一个值
    a    1   a1--a的第一个值
    a    3   a3:a的第三个值
    a    3   a3:a的第三个值
    b    1   b1--b的第一个值
    b    3   b3:b的第三个值
    b    2   b2b2b2b2
    b    4   b4b4
    b    5   b5b5b5b5b5
    */
    --在sql server 2000中只能用一个临时表来解决,生成一个自增列,先对val取最大或最小,然后再通过自增列来取数据。
    --创建表并插入数据:
    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',    1,   'a1--a的第一个值')
    insert into tb values('a',    3,   'a3: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')
    goselect * , px = identity(int,1,1) into tmp from tbselect m.name,m.val,m.memo from
    (
      select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
    ) m where px = (select min(px) from
    (
      select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
    ) n where n.name = m.name)drop table tb,tmp/*
    name       val         memo
    ---------- ----------- --------------------
    a          1           a1--a的第一个值
    b          1           b1--b的第一个值(2 行受影响)
    */
    --在sql server 2005中可以使用row_number函数,不需要使用临时表。
    --创建表并插入数据:
    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',    1,   'a1--a的第一个值')
    insert into tb values('a',    3,   'a3: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')
    goselect m.name,m.val,m.memo from
    (
      select * , px = row_number() over(order by name , val) from tb
    ) m where px = (select min(px) from
    (
      select * , px = row_number() over(order by name , val) from tb
    ) n where n.name = m.name)drop table tb/*
    name       val         memo
    ---------- ----------- --------------------
    a          1           a1--a的第一个值
    b          1           b1--b的第一个值(2 行受影响)
    */