日期(NCHAR) 号码1   号码2    序号   标记    日期(NCHAR) 号码1   号码2    序号   标记
  
20060101     A1      B1       2    M        
20060101     A1      B1       8    N     20060101     A1      B1       8    N
20060104     A2      B2       9    P
20060105     A2      B2       6    Q     20060105     A1      B1      6    Q要求:      当 左表中的 号码1+号码2相同,日期也相同时 ,输出序号大的那一行
            当 左表中的 号码1+号码2相同,但日期不同时 ,输出日期大的那一行如右图的结果  
刚开始学SQL  碰到个难题请帮忙
      

解决方案 »

  1.   


    select 日期,号码1,号码2,max(序号) as 序号, max(标记) as 标记  from table  group by 日期,号码1,号码2 having count(*)>1
    union all 
    select  max(a.日期) as 日期,a.号码1,a.号码2,max(a.序号) as 序号, max(a.标记) as 标记 from (select 日期,号码1,号码2,max(序号) as 序号, max(标记) as 标记 from table group by 日期,号码1,号码2 having count(*)=1) a group by a.号码1,a.号码2 
      

  2.   

    select t.* from tb t where 序号 = (select max(序号) from tb where 号码1 = t.号码1 and 号码2 = t.号码2 and 日期 = t.日期)
    union all
    select t.* from tb t where 日期 = (select max(日期) from tb where 号码1 = t.号码1 and 号码2 = t.号码2 and 日期 <> t.日期)
      

  3.   

    select * from Tab A where not exists (select 1 from Tab where A.号码1 = 号码1 and A.号码2 = 号码2 
    and A.日期= 日期 and 序号 > A.序号 )
    union all
    select * from Tab A where not exists (select 1 from Tab where A.号码1 = 号码1 and A.号码2 = 号码2 
    and A.日期 <>  日期 and 序号 > A.序号 )
      

  4.   

    2楼写错,不好意思.create table tb(日期 nvarchar(8), 号码1 varchar(10), 号码2 varchar(10), 序号 int, 标记 varchar(10))
    insert into tb values('20060101' , 'A1' , 'B1' , 2 , 'M') 
    insert into tb values('20060101' , 'A1' , 'B1' , 8 , 'N')
    insert into tb values('20060104' , 'A2' , 'B2' , 9 , 'P')
    insert into tb values('20060105' , 'A2' , 'B2' , 6 , 'Q')
    goselect m.* from
    (
      select t.* from tb t where 日期 = (select max(日期) from tb where 号码1 = t.号码1 and 号码2 = t.号码2 )
    ) m where 序号 = (select max(序号) from
    (
      select t.* from tb t where 日期 = (select max(日期) from tb where 号码1 = t.号码1 and 号码2 = t.号码2 )
    ) n where n.号码1 = m.号码1 and n.号码2 = m.号码2
    )drop table tb/*
    日期       号码1        号码2        序号          标记         
    -------- ---------- ---------- ----------- ---------- 
    20060101 A1         B1         8           N
    20060105 A2         B2         6           Q(所影响的行数为 2 行)
    */
      

  5.   


    create table #1(日期 nvarchar(8), 号码1 varchar(10), 号码2 varchar(10), 序号 int, 标记 varchar(10))
    insert into #1 values('20060101' , 'A1' , 'B1' , 2 , 'M') 
    insert into #1 values('20060101' , 'A1' , 'B1' , 8 , 'N')
    insert into #1 values('20060104' , 'A2' , 'B2' , 9 , 'P')
    insert into #1 values('20060105' , 'A2' , 'B2' , 6 , 'Q')
    go
    select a.* from #1 a
    join #1 b on a.号码1+a.号码2 = b.号码1+b.号码2
    where (a.日期 = b.日期 and a.序号>b.序号) or a.日期 > b.日期日期       号码1        号码2        序号          标记
    -------- ---------- ---------- ----------- ----------
    20060101 A1         B1         8           N
    20060105 A2         B2         6           Q(2 row(s) affected)
      

  6.   


    if object_id('tb')is not null
    drop table tbcreate table tb(日期 nvarchar(8), 号码1 varchar(10), 号码2 varchar(10), 序号 int, 标记 varchar(10))
    insert into tb values('20060101' , 'A1' , 'B1' , 2 , 'M') 
    insert into tb values('20060101' , 'A1' , 'B1' , 8 , 'N')
    insert into tb values('20060104' , 'A2' , 'B2' , 9 , 'P')
    insert into tb values('20060105' , 'A2' , 'B2' , 6 , 'Q')
    go;with tbcet
     as(
    select * from tb  a where not exists 
    (select  1 from tb where a.号码1=tb.号码1 and a.号码2=tb.号码2 and a.日期<tb.日期 )
    )
    ,
    tbcet1 as
    (
    select * from tbcet  a where not exists (select  *
    from tbcet where a.号码1=tbcet.号码1 and a.号码2=tbcet.号码2 and a.日期=tbcet.日期 and a.序号<tbcet.序号) 
    )select * from tbcet1插入一个我觉得好理解的sql,在建立cte的注意表的别名不要和数据库表名字一样.
      

  7.   

    日期(NCHAR)名称 号码1  号码2    序号  标记    日期(NCHAR)  名称 号码1  号码2    序号  标记 
      
    20060101     #    A1      B1      2    M        
    20060101     *    A1      B1      8    N    20060101      *     A1      B1      8    N 
    20060104     #    A2      B2      9    P 
    20060105     *    A2      B2      6    Q    20060105      *    A1      B1      6    Q 
    20070101     #    A1      B1      4    K
    20070102          A2      B2      7    L要求:      当 左表中的 号码1+号码2相同,日期也相同时 ,输出序号大的那一行 
                当 左表中的 号码1+号码2相同,但日期不同时 ,输出日期大的那一行 如右图的结果  
    刚开始学SQL  碰到个难题 
    忘记了  还有个条件 :只选名称为*的
      

  8.   


    declare @tb table (日期 nvarchar(10),号码1 nvarchar(10),号码2 nvarchar(10),序号 int,标记 nvarchar(5))
    insert into @tb select '20060101','A1','B1',2,'M'
          union all select '20060101','A1','B1',8,'N'
          union all select '20060104','A2','B2',9,'P'
          union all select '20060105','A2','B2',6,'Q'
    --select * from @tb a where 
    -- exists (select 1 from @tb where 号码1=a.号码1 and 号码2=a.号码2 )
    select * from 
    (select * from @tb a where not exists (select * from @tb where 号码1=a.号码1 and 号码2=a.号码2 and 日期>a.日期)) a 
    where not exists (select 1 from 
    (select * from @tb a where not exists (select * from @tb where 号码1=a.号码1 and 号码2=a.号码2 and 日期>a.日期))b where a.号码1=b.号码1 and b.序号>a.序号)  
    日期         号码1        号码2        序号          标记
    ---------- ---------- ---------- ----------- -----
    20060101   A1         B1         8           N
    20060105   A2         B2         6           Q(2 行受影响)   
      

  9.   

    日期(NCHAR)名称 号码1  号码2    序号  标记       日期(NCHAR)  名称    号码1    号码2      序号    标记 
      
    20060101    #    A1      B1      2    M        
    20060101    *    A1      B1      8    N       20060101       *      A1      B1         8       N 
    20060101         A1      B1      720060104    #    A2      B2      9    P 
    20060105    *    A2      B2      6    Q       20060105       *      A1      B1         6       Q 
    20070101    #    A1      B1      4    K 
    20070102         A2      B2      7    L 
    20090102    *    A1      B1      0    L       20090102       *      A1      B1         0       L
    20090102    ¥    A1       B1     5    K       20090102       ¥      A1      B1         5       K
    20090105    *    C1     D1       1            20090105        *     C1      D1          1     G要求:      当 左表中的 号码1+号码2相同,日期也相同时 ,输出序号大的那一行 
                当 左表中的 号码1+号码2相同,但日期不同时 ,输出日期大的那一行 
                当 左表中的 号码1+号码2+日期唯一时,也要该记录,不考虑序号
                大前提:只选 名称为*或者¥的,名称为空的、#的不要如右图的结果  
    刚开始学SQL  碰到个难题 
    忘记了  还有个条件 :只选名称为*的