有一表table1 项目名称 日期 单价
青菜1 207-8-1 12.2
青菜1 207-8-2 15.2
青菜2 207-8-2 38我要求显示的效果为
项目名称 日期 单价
青菜1 207-8-2 15.2
青菜2 207-8-2 38该SQL怎么写?
青菜1 207-8-1 12.2
青菜1 207-8-2 15.2
青菜2 207-8-2 38我要求显示的效果为
项目名称 日期 单价
青菜1 207-8-2 15.2
青菜2 207-8-2 38该SQL怎么写?
insert table1
select '青菜1','2007-08-01',12.2
union all
select '青菜1','2007-08-02',15.2
union all
select '青菜2','2007-08-02',38select * from table1 T1 where not exists(select top 1 * from table1 T2 where T2.日期>T1.日期)drop table table1
select a.*
from table1 a
inner join
(select 项目名称,max(日期) as 日期
from table1
group by 项目名称) b
on a.项目名称=b.项目名称 and a.日期=b.日期
where 日期=(select max(日期) from t where 项目名称=a. 项目名称)
這樣是不是更簡單
(select 项目名称,max(日期) 日期 frm table1 group by 项目名称) b
where a.项目名称 = b.项目名称 and a.日期 = b.日期
insert into @table select '青菜1','207-1-1','20.6'
union all select '青菜1','207-2-1','21.6'
union all select '青菜2','207-4-1','21.6'select a.* from @table a where not exists(select 1 from @table where [name]=a.[name] and [date]>a.[date])
from table1
group by 项目名称