0

我有一张如下所示的表格 -

  Id     Reference     DateAttribute1    DateAttribute2
  1      MMM005         2011-09-11       2012-09-10 
  2      MMM005         2012-06-13       2012-09-10 
  3      MMM006         2012-08-22       2012-09-10 
  4      MMM006         2012-08-22       2012-09-11 

我已经处理了 id 值。我想查询这样我得到以下结果

  Id     Reference     DateAttribute1    DateAttribute2
  2      MMM005         2012-06-13       2012-09-10 
  4      MMM006         2012-08-22       2012-09-11 

我希望我的结果按引用进行分组,然后是“DateAttribute1”,然后是“DateAttribute2”——正如您在上面的结果中看到的那样,DateAttribute1它具有优先级。 我应该如何编写查询以以上述方式获取结果?DateAttribute2

有什么解决办法吗?

4

4 回答 4

1
Try it.....

select * from (select * from your_table order by Id desc) as x group by Reference

于 2012-09-12T09:55:31.453 回答
1
select max(id),Reference, min(DateAttribute1),max(DateAttribute2)
group by Reference  
于 2012-09-12T09:27:40.947 回答
0

假设表名是'tbl2'

    select * from (select * from tbl2 order by REFERENCE,DateAttribute1 desc,
DateAttribute2 desc )  as abc group by reference
于 2012-09-12T10:45:40.840 回答
0

尝试这个:

select * 
from   your_table t
join (
      select Reference,
             min(DateAttribute1) as DateAttribute1,
             max(DateAttribute2) as DateAttribute2
      from   your_table
      group by  Reference
      )a
on   t.Reference=a.Reference
and  t.DateAttribute1=a.DateAttribute1
and  t.DateAttribute2=a.DateAttribute2        
于 2012-09-12T09:38:16.770 回答