有如下表:A,
CUS_NO     PRD_NO     S_DD            UP
======     =======    ==========     =====
  A        BAD001     2007-12-01      2.0
  A        BAD001     2008-01-21      2.2
  A        BAD001     2008-02-15      2.1
  B        BAD001     2008-01-11      2.2
  B        BAD001     2008-02-22      2.3
  C        BAD001     2008-02-21      2.2
  C        BAD001     2008-02-23      2.1
  A1       BAD002     2008-01-21      2.2
  A2       BAD002     2008-01-21      2.3
  A2       BAD002     2008-01-26      2.2
  ...      ...        ...             ...
例如:希望将BAD001对应CUS_NO中取A,BAD001,2008-02-15(最近日期)的记录
所以,希望最终结果如下:
CUS_NO     PRD_NO     S_DD            UP
======     =======    ==========     =====
  A        BAD001     2008-02-15      2.1
  B        BAD001     2008-02-22      2.3
  C        BAD001     2008-02-23      2.1
  A1       BAD002     2008-01-21      2.2
  A2       BAD002     2008-01-26      2.2
个人感觉比较难描述,就将需求结果列示出来让大家容易看懂。
请专家、高手帮忙!重谢!

解决方案 »

  1.   

    --按某一字段分组取最大(小)值所在行的数据
    (爱新觉罗.毓华 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 行受影响)
    */
      

  2.   


    select * from  a t where not exists(select 1 from a where CUS_NO=t.CUS_NO and PRD_NO>t.PRD_NO)
               
    --或者select * from a t where PRD_NO in(select max(PRD_NO) from a where CUS_NO=t.CUS_NO)
               
    --或者select * from a t where (select count(distinct PRD_NO) from a where CUS_NO=t.CUS_NO and PRD_NO>t.PRD_NO)=1
      

  3.   

    create table tb(CUS_NO CHAR(2),PRD_NO CHAR(6),S_DD DATETIME,UP DECIMAL(10,2))
    INSERT INTO TB SELECT     'A'    ,             'BAD001'          , '2007-12-01'  ,           2.0 
    INSERT INTO TB SELECT       'A'   ,              'BAD001'       ,    '2008-01-21' ,            2.2 
    INSERT INTO TB SELECT       'A'    ,             'BAD001'      ,     '2008-02-15'  ,           2.1 
    INSERT INTO TB SELECT       'B'     ,            'BAD001'     ,      '2008-01-11'   ,          2.2 
    INSERT INTO TB SELECT       'B'      ,           'BAD001'    ,       '2008-02-22'    ,         2.3 
    INSERT INTO TB SELECT       'C'       ,          'BAD001'   ,        '2008-02-21'     ,        2.2 
    INSERT INTO TB SELECT       'C'        ,         'BAD001'  ,         '2008-02-23'      ,       2.1 
    INSERT INTO TB SELECT       'A1'        ,       'BAD002'  ,         '2008-01-21'        ,     2.2 
    INSERT INTO TB SELECT       'A2'         ,      'BAD002' ,          '2008-01-21'         ,    2.3 
    INSERT INTO TB SELECT       'A2'          ,     'BAD002',           '2008-01-26'          ,   2.2SELECT * 
    FROM tb A
    WHERE NOT EXISTS(SELECT 1 FROM tb WHERE CUS_NO=A.CUS_NO AND S_DD>A.S_DD)DROP TABLE tb/*
    CUS_NO PRD_NO S_DD                    UP
    ------ ------ ----------------------- ---------------------------------------
    A      BAD001 2008-02-15 00:00:00.000 2.10
    B      BAD001 2008-02-22 00:00:00.000 2.30
    C      BAD001 2008-02-23 00:00:00.000 2.10
    A1     BAD002 2008-01-21 00:00:00.000 2.20
    A2     BAD002 2008-01-26 00:00:00.000 2.20(5 row(s) affected)
    */
      

  4.   

    解决了,请结帖
    结帖方式:管理帖子->给分->输入密码->结帖.
    谢谢.
      

  5.   

    select * from A where not exists(select * from A b where a.cus_no=b.cus_no and a.prd_no=b.prd_no and a.s_dd<b.s_dd) order by len(cus_no),cus_no
      

  6.   

    3楼的回复,如果存在如下情况
    create table tb(CUS_NO CHAR(2),PRD_NO CHAR(6),S_DD DATETIME,UP DECIMAL(10,2))
    INSERT INTO TB SELECT     'A'    ,             'BAD001'          , '2007-12-01'  ,           2.0 
    INSERT INTO TB SELECT       'A'   ,              'BAD001'       ,    '2008-01-21' ,            2.2 
    INSERT INTO TB SELECT       'A'    ,             'BAD001'      ,     '2008-02-15'  ,           2.1 
    INSERT INTO TB SELECT       'B'     ,            'BAD001'     ,      '2008-01-11'   ,          2.2 
    INSERT INTO TB SELECT       'B'      ,           'BAD001'    ,       '2008-02-22'    ,         2.3 
    INSERT INTO TB SELECT       'C'       ,          'BAD001'   ,        '2008-02-21'     ,        2.2 
    INSERT INTO TB SELECT       'C'        ,         'BAD001'  ,         '2008-02-23'      ,       2.1 
    INSERT INTO TB SELECT       'A1'        ,       'BAD002'  ,         '2008-01-21'        ,     2.2 
    INSERT INTO TB SELECT       'A2'         ,      'BAD002' ,          '2008-01-21'         ,    2.3 
    INSERT INTO TB SELECT       'A2'          ,     'BAD002',           '2008-01-26'          ,   2.3  --多增加一条记录
    INSERT INTO TB SELECT       'A2'          ,     'BAD002',           '2008-01-26'          ,   2.2SELECT * 
    FROM tb A
    WHERE NOT EXISTS(SELECT 1 FROM tb WHERE CUS_NO=A.CUS_NO AND S_DD>A.S_DD)DROP TABLE tb则存在问题
      

  7.   

    select * from a a1 
    where a1.s_dd=(select max(a2.s_dd) from a a2 where a2.cus_no=a1.cus_no)
      

  8.   

    SDHYLJ  青锋-SS
    select   *   from   A  (此处添加a)where   not   exists(select   *   from   A   b   where   a.cus_no=b.cus_no   and   a.prd_no=b.prd_no   and   a.s_dd <b.s_dd)   order   by   len(cus_no),cus_no
    即可。谢谢你。
    真不知道如何给分合适。
      

  9.   

    create table A(CUS_NO varchar(8),PRD_NO varchar(10),S_DD datetime,UP decimal(9,2))
    go
    insert into A
    select 'A','BAD001','2007-12-01',2.0
    union select 'A','BAD001','2008-01-21',2.2
    union select 'A','BAD001','2008-02-15',2.1
    union select 'B','BAD001','2008-01-11',2.2
    union select 'B','BAD001','2008-02-22',2.3
    union select 'C','BAD001','2008-02-21',2.2
    union select 'C','BAD001','2008-02-23',2.1
    union select 'A1','BAD002','2008-01-21',2.2
    union select 'A2','BAD002','2008-01-21',2.3
    union select 'A2','BAD002','2008-01-26',2.3
    union select 'A2','BAD002','2008-01-26',2.2
    go
    select * from A;
    select cus_no,prd_no,s_dd,up 
    from A 
    where not exists(select * from A b where a.cus_no=b.cus_no and a.prd_no=b.prd_no and checksum(a.s_dd,a.up)>checksum(b.s_dd,b.up)) 
    order by len(cus_no),cus_no
    go
    drop table A;
    go(11 行受影响)
    CUS_NO   PRD_NO     S_DD                    UP
    -------- ---------- ----------------------- ---------------------------------------
    A        BAD001     2007-12-01 00:00:00.000 2.00
    A        BAD001     2008-01-21 00:00:00.000 2.20
    A        BAD001     2008-02-15 00:00:00.000 2.10
    A1       BAD002     2008-01-21 00:00:00.000 2.20
    A2       BAD002     2008-01-21 00:00:00.000 2.30
    A2       BAD002     2008-01-26 00:00:00.000 2.20
    A2       BAD002     2008-01-26 00:00:00.000 2.30
    B        BAD001     2008-01-11 00:00:00.000 2.20
    B        BAD001     2008-02-22 00:00:00.000 2.30
    C        BAD001     2008-02-21 00:00:00.000 2.20
    C        BAD001     2008-02-23 00:00:00.000 2.10(11 行受影响)cus_no   prd_no     s_dd                    up
    -------- ---------- ----------------------- ---------------------------------------
    A        BAD001     2008-02-15 00:00:00.000 2.10
    B        BAD001     2008-02-22 00:00:00.000 2.30
    C        BAD001     2008-02-23 00:00:00.000 2.10
    A1       BAD002     2008-01-21 00:00:00.000 2.20
    A2       BAD002     2008-01-21 00:00:00.000 2.30(5 行受影响)
      

  10.   

    这样更好些
    select cus_no,prd_no,s_dd,up 
    from A 
    where not exists(select * from A b where a.cus_no=b.cus_no and a.prd_no=b.prd_no and checksum(a.s_dd,a.up)>checksum(b.s_dd,b.up)) 
    order by len(cus_no),cus_no
    另外,你看看我9楼的解释.
      

  11.   

    好像  “SDHYLJ     青锋-SS”给的SQL和我写的比较一致。
    不知道我是否在哪个地方少考虑了。呵呵
      

  12.   


    -----看错字段了。晕死.
    select * from  a t where not exists(select 1 from a where CUS_NO=t.CUS_NO and PRD_NO=t.PRD_NO and s_dd>t.S_DD)
               
    --或者select * from a t where S_DD in(select max(S_DD) from a where CUS_NO=t.CUS_NO and PRD_NO=t.PRD_NO )
               
    --或者select * from a t where (select count(distinct S_DD) from a where CUS_NO=t.CUS_NO and PRD_NO=t.PRD_NO and s_dd>t.S_DD)=1
      

  13.   

    解决了,请结帖 
    结帖方式:管理帖子-> 给分-> 输入密码-> 结帖. 
    谢谢.请不要点帖子加分.
      

  14.   

    不会结贴请参考如下帖子:
    http://topic.csdn.net/u/20080110/19/7cb462f1-cac6-4c28-848e-0a879f4fd642.html
      

  15.   

        * hww2004
        * 賤客無名
        * 等 级:
    发表于:2008-02-22 17:45:0316楼 得分:0
    不好意思,我发贴时,没有去看我的分数,现在看才知道有60分,关键是我现在去结贴给分时,系统不让我结贴,请问:我该如何去操作,谢谢。
    ---------
    楼主可能没注意,这个帖子实际是100分的帖子.
      

  16.   

    http://topic.csdn.net/u/20080110/19/7cb462f1-cac6-4c28-848e-0a879f4fd642.html这个帖子讲述了详细的结贴方法
      

  17.   

    LZ 不会结贴我教你:

    是这样子的....                   在帖子列表中找到本帖,看到帖子最后有“管理”了吗? 
    第一步:   用你可爱的鼠标点击一下。                       进入帖子后找到23楼,一定要记住是23楼,看到得分后面有一个输入框没有? 
    第二步:   输入100                   再往上找,可以看到在1楼的上面有一个广告,广告上面的左边有什么呀? 
    第三步:   在请输入密码后面的输入框内输入你的CSDN的登陆密码。                     往右边看看,是不是有一个按钮呢? 
    第四步:   点击   结帖 
      

  18.   

    select * 
    from  a t 
    where not exists(select 1 
             from a 
             where CUS_NO=t.CUS_NO and PRD_NO=t.PRD_NO and s_dd>t.S_DD)