create table table1 (proid int,proname varchar(10))
insert into table1 values('1','a')
insert into table1 values('2','b')create table table2 (proid int,proarea varchar(10),proprice smallmoney)
insert into table2 values('1','BJ',10)
insert into table2 values('1','SH',8)
insert into table2 values('1','GZ',2)
insert into table2 values('2','BJ',1)
insert into table2 values('2','SH',3) 我想得到每个产品的最低价格和最低价格所在地区以及产品信息,
如有同一ProID有两条价格相等的最低价,则只显示一条 Name Area Pric  
 
select proname,min(proprice) from table1 t1 join table2 t2 on t1.proid=t2.proid 
group by proname order by min(proprice)-------可以正确查出来最低名和价格select proname,proarea,min(proprice) from table1 t1 join table2 t2 on t1.proid=t2.proid 
group by proname,proarea order by min(proprice)----查出全表出来了,就多了个字段,不知道为什么还有一问,能不能使用TOP N来每个组的前N行。好像TOP只能取表的前N行 谢谢达人指点

解决方案 »

  1.   

    create   table   table1   (proid   int,proname   varchar(10)) 
    insert   into   table1   values( '1 ', 'a ') 
    insert   into   table1   values( '2 ', 'b ') create   table   table2   (proid   int,proarea   varchar(10),proprice   smallmoney) 
    insert   into   table2   values( '1 ', 'BJ ',10) 
    insert   into   table2   values( '1 ', 'SH ',8) 
    insert   into   table2   values( '1 ', 'GZ ',2) 
    insert   into   table2   values( '2 ', 'BJ ',1) 
    insert   into   table2   values( '2 ', 'SH ',3)   
    goselect 
    t2.proname,t.* 
    from 
    table2 t 
    join
    table1 t2 on t.proid=t2.proid
    where 
    not exists(select 1 from table2 where proid=t.proid and proprice<t.proprice)proname    proid       proarea    proprice     
    ---------- ----------- ---------- ------------ 
    a          1           GZ         2.0000
    b          2           BJ         1.0000(所影响的行数为 2 行)
      

  2.   

    select 
    t2.proname,t.* 
    from 
    table2 t 
    join
    table1 t2 on t.proid=t2.proid
    where
    t.proprice=(select  min(proprice) from table2 where proid=t.proid )
    proname    proid       proarea    proprice     
    ---------- ----------- ---------- ------------ 
    a          1           GZ         2.0000
    b          2           BJ         1.0000(所影响的行数为 2 行)
      

  3.   

    select 
    t2.proname,min(t.proprice) as proprice
    from 
    table2 t 
    join
    table1 t2 on t.proid=t2.proid
    group by t2.pronameproname    proprice     
    ---------- ------------ 
    a          2.0000
    b          1.0000(所影响的行数为 2 行)
      

  4.   

    好像TOP只能取表的前N行??
    --------同一产品的最低的几记录,有没有去掉重复create   table   table1   (proid   int,proname   varchar(10)) 
    insert   into   table1   values( '1 ', 'a ') 
    insert   into   table1   values( '2 ', 'b ') create   table   table2   (proid   int,proarea   varchar(10),proprice   smallmoney) 
    insert   into   table2   values( '1 ', 'BJ ',10) 
    insert   into   table2   values( '1 ', 'SH ',8) 
    insert   into   table2   values( '1 ', 'GZ ',2) 
    insert   into   table2   values( '2 ', 'BJ ',1) 
    insert   into   table2   values( '2 ', 'SH ',3)   
    goselect 
    t2.proname,t.*
    from 
    table2 t 
    join
    table1 t2 on t.proid=t2.proid
    where
    (select count(1) from table2 where proid=t.proid and proprice<t.proprice)<2--前两条
    proname    proid       proarea    proprice     
    ---------- ----------- ---------- ------------ 
    a          1           SH         8.0000
    a          1           GZ         2.0000
    b          2           BJ         1.0000
    b          2           SH         3.0000(所影响的行数为 4 行)
      

  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
    */
      

  6.   

    create   table   table1   (proid   int,proname   varchar(10)) 
    insert   into   table1   values( '1 ', 'a ') 
    insert   into   table1   values( '2 ', 'b ') create   table   table2   (proid   int,proarea   varchar(10),proprice   smallmoney) 
    insert   into   table2   values( '1 ', 'BJ ',10) 
    insert   into   table2   values( '1 ', 'SH ',8) 
    insert   into   table2   values( '1 ', 'GZ ',2) 
    insert   into   table2   values( '2 ', 'BJ ',1) 
    insert   into   table2   values( '2 ', 'SH ',3)   select table1.* , isnull(t.proarea,'') proarea,isnull(t.proprice,0) proprice from table1,
    (select * from table2 a where proprice = (select min(proprice) from table2 where proid = a.proid)) t
    where table1.proid = t.proiddrop table table1,table2/*
    proid       proname    proarea    proprice     
    ----------- ---------- ---------- ------------ 
    1           a          GZ         2.0000
    2           b          BJ         1.0000(所影响的行数为 2 行)
    */
      

  7.   

    create   table   table1   (proid   int,proname   varchar(10)) 
    insert   into   table1   values( '1 ', 'a ') 
    insert   into   table1   values( '2 ', 'b ') create   table   table2   (proid   int,proarea   varchar(10),proprice   smallmoney) 
    insert   into   table2   values( '1 ', 'BJ ',10) 
    insert   into   table2   values( '1 ', 'SH ',8) 
    insert   into   table2   values( '1 ', 'GZ ',2) 
    insert   into   table2   values( '2 ', 'BJ ',1) 
    insert   into   table2   values( '2 ', 'SH ',3)   
    goselect table1.* , isnull(t.proarea,'') proarea,isnull(t.proprice,0) proprice from table1
    left join
    (select * from table2 a where proprice = (select min(proprice) from table2 where proid = a.proid)) t
    on table1.proid = t.proid
    order by table1.proiddrop table table1,table2/*
    proid       proname    proarea    proprice     
    ----------- ---------- ---------- ------------ 
    1           a          GZ         2.0000
    2           b          BJ         1.0000(所影响的行数为 2 行)
    */
      

  8.   

    create   table   table1   (proid   int,proname   varchar(10)) 
    insert   into   table1   values( '1 ', 'a ') 
    insert   into   table1   values( '2 ', 'b ') create   table   table2   (proid   int,proarea   varchar(10),proprice   smallmoney) 
    insert   into   table2   values( '1 ', 'BJ ',10) 
    insert   into   table2   values( '1 ', 'SH ',8) 
    insert   into   table2   values( '1 ', 'GZ ',2) 
    insert   into   table2   values( '2 ', 'BJ ',1) 
    insert   into   table2   values( '2 ', 'SH ',3)   
    go--按proid分组取最大的两个(N个)proprice
    select table1.* , isnull(t.proarea,'') proarea,isnull(t.proprice,0) proprice from table1
    left join
    (select a.* from table2 a where 2 > (select count(*) from table2 where proid = a.proid and proprice > a.proprice )) t
    on table1.proid = t.proid
    order by table1.proid,propricedrop table table1,table2/*
    proid       proname    proarea    proprice     
    ----------- ---------- ---------- ------------ 
    1           a          SH         8.0000
    1           a          BJ         10.0000
    2           b          BJ         1.0000
    2           b          SH         3.0000
    */
      

  9.   

    top n,对结果集打上括号作为一子表,对此子表进行查询.