求一sql语句,加急。。。

txgaozhao 2009-02-06 06:33:35
有一个表 news,结构如下
id type update
1 1 2009-01-01
2 1 2009-01-02
3 2 2009-01-03
4 3 2009-01-04
5 3 2009-01-05
6 3 2009-01-06
7 2 2009-01-07
8 2 2009-01-08

要获取表的每种类型中update最新的一条数据,请问如何实现?
就是要获得如下的结果
id type update
2 1 2009-01-02
8 2 2009-01-08
6 3 2009-01-06
...全文
192 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
woshidaniu 2009-02-24
  • 打赏
  • 举报
回复
好像用“select top 1* from table_name order by date desc"可以把,我试过了,ok
amandag 2009-02-06
  • 打赏
  • 举报
回复
才看到题目就这么多答案了..
wndsc 2009-02-06
  • 打赏
  • 举报
回复
学习一下
飞飞虫2015 2009-02-06
  • 打赏
  • 举报
回复
我是来顶帖的
chengqscjh 2009-02-06
  • 打赏
  • 举报
回复

if object_id('news') is not null drop table news
create table news (ID int identity(1,1),[type] int,[update] datetime)
insert into news
select 1,'2009-01-01' union all
select 1,'2009-01-02' union all
select 2 ,'2009-01-03' union all
select 3 ,'2009-01-04' union all
select 3,'2009-01-05' union all
select 3 ,'2009-01-06' union all
select 2 ,'2009-01-07' union all
select 2 ,'2009-01-08'


select * from news aa
where ID in (select top 1 ID from news where [type]=aa.[type] order by [update] desc)
order by aa.[type]
hack8 2009-02-06
  • 打赏
  • 举报
回复
答案很多呀,给出一个思路
select top + order by + group by ,不愿意写出全部代码了,对成长不利。
hongmaohouzi 2009-02-06
  • 打赏
  • 举报
回复
select * from news aa
where id in (select top 1 id from news where type=aa.type order by update desc)
order by aa.type
HDNGO 2009-02-06
  • 打赏
  • 举报
回复
如果时间有重复。。。觉得应该ID倒序,大的应该是后加的吧。。。

select * from @news aa
where id in (select top 1 id from @news where type=aa.type order by [update] desc)
order by aa.type asc,aa.id desc
HDNGO 2009-02-06
  • 打赏
  • 举报
回复
declare @news table(
[id] [int] IDENTITY (1, 1) NOT NULL ,
[type] [int] NOT NULL ,
[update] [datetime] NOT NULL
)

insert into @news
select 1,'2009-1-6'
union select 2,'2009-2-4'
union select 3,'2009-2-2'
union select 2,'2009-2-3'
union select 1,'2009-2-1'
union select 2,'2009-1-4'
union select 3,'2009-1-5'

select * from @news aa
where id in (select top 1 id from @news where type=aa.type order by [update] desc)
order by aa.type

/*结果
2 1 2009-02-01 00:00:00.000
5 2 2009-02-04 00:00:00.000
7 3 2009-02-02 00:00:00.000
*/
HDNGO 2009-02-06
  • 打赏
  • 举报
回复
改一个关键字加上方括号~~

select * from news aa
where id in (select top 1 id from news where type=aa.type order by [update] desc)
order by aa.type
ljhcy99 2009-02-06
  • 打赏
  • 举报
回复
1.
select * from table,

(select max(update) update ,type
from table
group by type) A
where a.update =table.update
and a.type=table.type
order by table.type
2,
select id, type, update,
from
(
select *,row_number()over(partition by type order by update desc) as cnt
from table) a
where cnt=1
别样苍茫 2009-02-06
  • 打赏
  • 举报
回复

select * from Table_1 t1 where [update] =
(select max([update]) from Table_1 t2 where t2.[type]=t1.[type])
and id=(select MAX(id) from Table_1 t3 where t3.[type]=t1.[type])
order by id

HDNGO 2009-02-06
  • 打赏
  • 举报
回复
每个类根据排序取前N条。。。。

select * from news aa
where id in (select top 1 id from news where type=aa.type order by update desc)
order by aa.type
HDNGO 2009-02-06
  • 打赏
  • 举报
回复
select * from news aa
where id in (select top 1 id from news where type=aa.type order by update desc)
order by aa.type
我姓区不姓区 2009-02-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 txgaozhao 的回复:]
update有重复的怎么办?
貌似这个ok了
select distinct type, id ,max(update) from news n1 group by type,id
[/Quote]
你这个可以吗?应该不可以吧
我姓区不姓区 2009-02-06
  • 打赏
  • 举报
回复

select * from Table_1 t1 where [update] =
(select max([update]) from Table_1 t2 where t2.[type]=t1.[type])
and id=(select MAX(id) from Table_1 t3 where t3.[type]=t1.[type])
order by id
txgaozhao 2009-02-06
  • 打赏
  • 举报
回复
update有重复的怎么办?
貌似这个ok了
select distinct type, id ,max(update) from news n1 group by type,id
我姓区不姓区 2009-02-06
  • 打赏
  • 举报
回复

select * from Table_1 t1 where [update] =
(select max([update]) from Table_1 t2 where t2.[type]=t1.[type])
order by id
txgaozhao 2009-02-06
  • 打赏
  • 举报
回复
datetime 型的
我姓区不姓区 2009-02-06
  • 打赏
  • 举报
回复
update 是datetime型的还是字符型的?

62,269

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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