日期:2014-05-18 浏览次数:20621 次
create table #tb (
品名 varchar(20),
价格 int,
日期 date
)
insert into #tb select '商品A', 10, '2011-01-05'
union all select '商品B', 5, '2011-02-01'
union all select '商品A', 8, '2011-03-02'
union all select '商品C', 7, '2011-03-05'
union all select '商品C', 8, '2011-04-05'
go
select a.品名,b.价格,a.日期 from (select 品名,MAX(日期)日期
from #tb group by 品名) a right join #tb b on a.品名=b.品名 and a.日期=b.日期 where a.品名 is not null
order by a.品名
结果为:
品名 价格 日期
商品C 8 2011-04-05
商品B 5 2011-02-01
商品A 8 2011-03-02