2016-08-18 77 views
1

的基础上我有一种情况,其中,我有获取最小列的其他字段

Id|rank| date 
1 | 7 |07/08/2015 
1 | 7 |09/08/2015 
1 | 8 |16/08/2015 
1 | 8 |17/08/2015 
1 | 7 |19/08/2015 
1 | 7 |15/08/2015 
2 | 7 |01/08/2015 
2 | 7 |02/08/2015 
2 | 8 |16/08/2015 
2 | 8 |17/08/2015 
2 | 7 |26/08/2015 
2 | 7 |28/08/2015 

我所需的解决方案是

1 | 7 |07/08/2015 
1 | 8 |16/08/2015 
1 | 7 |15/08/2015 
2 | 7 |01/08/2015 
2 | 8 |16/08/2015 
2 | 7 |26/08/2015 

即用于我想id和秩的每个块最短的日期。 我曾尝试使用while循环,因为有成千上万的记录它需要2小时load.Is有任何其他方式做请请建议。

+0

搜索类似“组SQL第一行”这是一个:http://stackoverflow.com/questions/3800551/select-first-row-in-each-group-按组 – shawnt00

+1

为什么1,7,15/08/2015为07/08和15/08重复两次一次两次 – TheGameiswar

+0

不重复... 1和7重复,因为我想输出特定批次的ID和排名 – user2941762

回答

0

这里是使用查询ROW_NUMBER()

;WITH cte_rec 
    as (SELECT Id,Rank,Date 
        ,ROW_NUMBER()OVER (partition by Id,Rank ORDER BY date) as RNO 
     FROM YourTable) 
     SELECT Id,Rank,Date 
     FROM cte_rec 
     WHERE RNO =1 
+0

谢谢unni,但这不会达到欲望的结果 – user2941762

1

对于每一行利用必要为了得到唯一的行号。 (因为我比ID更重要,日期比排名更重要)。

使用移动了一行的行号(d1.RowNum = d2.RowNum+1)将结果表加入其自身。

只选择加入“其他块”行的行(d1.Id <> d2.Id or d1.Rank <> d2.rank)。

根据换档方向和选定的表格选择最大或最小日期。

不要忘记“边缘案例” - 由于移位不能加入的行(这就是为什么不使用inner joind1.RowNum = 1条件)。

;WITH dataWithRowNums as (
    select Id, Rank, Date, 
     RowNum = ROW_NUMBER() OVER (ORDER BY Id,date,rank) 
    from YourTable 
) 
select d1.Id, d1.Rank, d1.Date 
from dataWithRowNums d1 
left join dataWithRowNums d2 
    on d1.RowNum = d2.RowNum+1 and (d1.Id <> d2.Id or d1.Rank <> d2.rank) 
where not d2.Id is null or d1.RowNum = 1 

此代码返回结果位不同从你:

Id Rank Date 
1 7 2015-08-07 
1 8 2015-08-16 
1 7 2015-08-19 <-- you've got here 2015-08-15 
2 7 2015-08-01 
2 8 2015-08-16 
2 7 2015-08-26 

作为块(等级8编号1)在16/08已经开始因此行15/08用于秩7是有关第一个块(等级7 Id1)。

如果您仍然需要你的排序(所以15/08等级7是与第二块(rank7 ID1)),那么你应该提供自己的RowSorting数据,然后在这里问关于另一个任务的另一个解决方案)

0
This is what I have tried and is running as expected 
create table #temp 
    (
      iden int identity(1,1), 
      ID int, 
      [rank] int, 
      [date] date, 
      dr_id int, 
      rownum_id int, 
      grouprecord int 
    ) 


    Insert into #temp(id,rank,date) 
    select 1 , 7 ,'07/08/2015' 
    union all select 1 , 7 ,'09/08/2015' 
    union all select 1 , 8 ,'08/16/2015' 
    union all select 1 , 8 ,'08/17/2015' 
    union all select 1 , 7 ,'08/19/2015' 
    union all select 1 , 7 ,'08/15/2015' 
    union all select 2 , 7 ,'08/01/2015' 
    union all select 2 , 7 ,'08/02/2015' 
    union all select 2 , 8 ,'08/16/2015' 
    union all select 2 , 8 ,'08/17/2015' 
    union all select 2 , 7 ,'08/26/2015' 
    union all select 2 , 7 ,'08/28/2015' 


    update t1 
    set dr_id = t2.rn 
    from #temp t1 inner join 
    (select iden, dense_rank() over(order by id) as rn from #temp) t2 
    on t1.iden = t2.iden 


    update t1 
    set rownum_id = t2.rn 
    from #temp t1 inner join 
    (select iden, row_number() over(partition by dr_id order by id) as rn from #temp) t2 
    on t1.iden = t2.iden 




    select *,row_number() over(order by iden)rn into #temp1 from 
    (    
         select t2.* 
        from #temp t1 inner join #temp t2 
        on (t1.dr_id = t2.dr_id or t2.dr_id = (t1.dr_id +1)) and (t1.rank<>t2.rank or t2.dr_id = (t1.dr_id +1)) 
        and t2.iden = t1.iden + 1 
    )a 





    declare @id int,@miniden int,@maxiden int,@maxid int 
    set @id = 1 
    select @maxid = max(iden) from #temp 

    while exists(select 1 from #temp1 where rn = @id) 
    begin 
      Select @miniden = iden from #temp1 
      where rn = @id 

      Select @maxiden = iden from #temp1 
      where rn = @id+1 

      update #temp 
      set grouprecord = @id +1 
      where iden between @miniden and @maxiden 

    IF(@maxiden IS NULL) 
    BEGIN 
      Update #temp 
      set grouprecord = @id +1 
      where iden between @miniden and @maxid 
    END 

      set @id = @id + 1 
      SET @miniden =NULL 
      SET @maxiden = NULL 
    end 

    UPDATE #TEMP 
    SET GROUPRECORD = 1 
    WHERE GROUPRECORD IS NULL 



    select min(date) as mindate,grouprecord from #temp 
    group by grouprecord 

谢谢大家的帮助:)

相关问题