一个通过年月取最新数据的问题

shenqdm 2010-08-05 05:21:42
A表如下
resourceId ,country, year ,month ,value
1, china , 2009, 1, 1
1, china, 2010, 5, 3
1, china, 2009, 10, 2
2, china, 2010, 1, 6

现在给你的 条件是 resourceId =1 ,country=china,
怎么样找到最新的那条数据 ,即1, china, 2010, 5, 3
...全文
69 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
支持1楼
Johnson 2010-08-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xys_777 的回复:]
select top 1 * from A
where resourceId =1 and country='china'
order by [year] desc,[month] desc
[/Quote]
liuchuan_beijing 2010-08-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xys_777 的回复:]
select top 1 * from A
where resourceId =1 and country='china'
order by [year] desc,[month] desc
[/Quote]
up~~~
华夏小卒 2010-08-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xys_777 的回复:]
select top 1 * from A
where resourceId =1 and country='china'
order by [year] desc,[month] desc
[/Quote]我的思维还是比较局限哦
华夏小卒 2010-08-05
  • 打赏
  • 举报
回复
--> 测试数据: #tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb (resourceId int,country varchar(5),year int,month int,value int)
insert into #tb
select 1,'china',2009,1,1 union all
select 1,'china',2010,5,3 union all
select 1,'china',2009,10,2 union all
select 2,'china',2010,1,6

select top 1 * from #tb
where resourceId =1 and country='china'
order by ltrim(year)+'-'+ltrim(month)+'-01' desc


resourceId country year month value
----------- ------- ----------- ----------- -----------
1 china 2010 5 3

(1 行受影响)
csuxp2008 2010-08-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 csuxp2008 的回复:]
SQL code
select top 1*
from a
order by year desc,month desc,value desc

可以不?
[/Quote]

忘记where条件了 - -
csuxp2008 2010-08-05
  • 打赏
  • 举报
回复
select top 1*
from a
order by year desc,month desc,value desc

可以不?
永生天地 2010-08-05
  • 打赏
  • 举报
回复
select top 1 * from A
where resourceId =1 and country='china'
order by [year] desc,[month] desc

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧