日期(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  碰到个难题 
忘记了  还有个条件 :只选名称为*的 

解决方案 »

  1.   


    -->第一题:
    select * 
    from 左表 a 
      left join (select [日期(NCHAR)],号码1,号码2,max(序号) 序号 from 左表 group by 日期(NCHAR),号码1,号码2) b
        on a.序号=b.序号
    -->第二题:
    select* 
    from 左表 a
      left join (select max([日期(NCHAR)]) 日期,号码1,号码2,max(序号) 序号 from 左表 group by 号码1,号码2) b 
        on a.日期=b.日期 and a.序号=b.序号
      

  2.   


    -->忘记了  还有个条件 :只选名称为*的
    -->第一题:
    select * 
    from 左表 a 
      left join (select [日期(NCHAR)],号码1,号码2,max(序号) 序号 from 左表 group by 日期(NCHAR),号码1,号码2) b
        on a.序号=b.序号
    where a.名称='*'
    -->第二题:
    select* 
    from 左表 a
      left join (select max([日期(NCHAR)]) 日期,号码1,号码2,max(序号) 序号 from 左表 group by 号码1,号码2) b 
        on a.日期=b.日期 and a.序号=b.序号
    where a.名称='*'
      

  3.   

    [code=SQL
    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 行受影响)   
     
     
    修改 删除 举报 引用 回复   [/code]
      

  4.   


    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