sql分组后取最大日期的记录
现有表:createtabletempTest(f_Spbmvarchar(50)notnull,--商品编码f_Rqvarchar(50)notnull,--日期f_K...
现有表:
create table tempTest
(
f_Spbm varchar(50) not null,--商品编码
f_Rq varchar(50) not null, --日期
f_Kcsl float not null --库存数量
)
go
insert tempTest(f_Spbm,f_Rq,f_Kcsl)
select '1501050049','20130718',22 union
select '1501050049','20130719',21 union
select '1501050049','20130720',20 union
select '1501050049','20130721',19 union
select '1501050049','20130722',18
---------------------------------------------------------------
通过 select * from tempTest 得到
f_Spbm f_Rq f_Kcsl
1501050049 20130718 22.0
1501050049 20130719 21.0
1501050049 20130720 20.0
1501050049 20130721 19.0
1501050049 20130722 18.0
---------------------------------------------------------------
现在我想查到最大日期的商品编码和库存数量
显示结果为(日期不显示):
---------------------------------------------------------------
f_Spbm f_Kcsl
1501050049 18.0
---------------------------------------------------------------
求语句? 展开
create table tempTest
(
f_Spbm varchar(50) not null,--商品编码
f_Rq varchar(50) not null, --日期
f_Kcsl float not null --库存数量
)
go
insert tempTest(f_Spbm,f_Rq,f_Kcsl)
select '1501050049','20130718',22 union
select '1501050049','20130719',21 union
select '1501050049','20130720',20 union
select '1501050049','20130721',19 union
select '1501050049','20130722',18
---------------------------------------------------------------
通过 select * from tempTest 得到
f_Spbm f_Rq f_Kcsl
1501050049 20130718 22.0
1501050049 20130719 21.0
1501050049 20130720 20.0
1501050049 20130721 19.0
1501050049 20130722 18.0
---------------------------------------------------------------
现在我想查到最大日期的商品编码和库存数量
显示结果为(日期不显示):
---------------------------------------------------------------
f_Spbm f_Kcsl
1501050049 18.0
---------------------------------------------------------------
求语句? 展开
展开全部
select a.f_SPBM,a.f_kcsl
from tempTest a,
(
select f_SPBM,max(f_rq) f_rq
from tempTest
group by f_SPBM
) b
where a. f_SPBM = b. f_SPBM
and a.f_rq = b.f_rq
from tempTest a,
(
select f_SPBM,max(f_rq) f_rq
from tempTest
group by f_SPBM
) b
where a. f_SPBM = b. f_SPBM
and a.f_rq = b.f_rq
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
为什么要分组呢,下面唯罩肢的指世就闷袭可以:
select f_spbm,f_kcsl from test where f_rq=(select max(f_rq) from test);
select f_spbm,f_kcsl from test where f_rq=(select max(f_rq) from test);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询