当前位置: 代码迷 >> Sql Server >> 编程越来越差了.简单的SQL语句.
  详细解决方案

编程越来越差了.简单的SQL语句.

热度:207   发布时间:2016-04-27 19:28:21.0
编程越来越差了.求一个简单的SQL语句.....
编程越来越差了.求一个简单的SQL语句..... 

各位,有如下表:

id |类型 | 内容 | 发表时间
1 |A | 12344455 | 2007-9-10
2 |B | 34223424 | 2007-9-12
3 |C | 11111111 | 2007-9-13
1 |A | 12344455 | 2007-10-10
2 |B | 34223424 | 2007-10-12

即根据发表时间取得每个类型的最新的一条记录.

请教各位大侠,谢谢啦!~~

应该看懂了吧?


------解决方案--------------------
SQL code
select * from 表 a where 发表时间=(select max(发表时间) from 表 where 类型=a.类型)
------解决方案--------------------
SQL code
--原始数据:@Tdeclare @T table(id int,类型 varchar(8),内容 int,发表时间 datetime)insert @Tselect 1,'&brvbarA',12344455,'2007-9-10' union allselect 2,'&brvbarB',34223424,'2007-9-12' union allselect 3,'&brvbarC',11111111,'2007-9-13' union allselect 1,'&brvbarA',12344455,'2007-10-10' union allselect 2,'&brvbarB',34223424,'2007-10-12'select *
------解决方案--------------------
SQL code
--可以使用cross applycreate function dbo.myfunc(@id as int)returns tableasreturn select top(1) *  from dbo.testwhere [email protected]order by 发表时间 desc;goselect b.id,b.类型,b.内容,b.发表时间from dbo.test  a cross  apply dbo.myfunc(a.id) as bgroup by b.id,b.类型,b.内容,b.发表时间;go-------------------id          类型       内容          发表时间----------- -------- ----------- -----------------------1           A        12344455    2007-10-10 00:00:00.0002           B        34223424    2007-10-12 00:00:00.0003           C        11111111    2007-09-13 00:00:00.000(3 row(s) affected)
------解决方案--------------------
create table T(id int,类型 varchar(8),内容 int,发表时间 datetime)
insert T
select 1,'&brvbarA',12344455,'2007-9-10' union all
select 2,'&brvbarB',34223424,'2007-9-12' union all
select 3,'&brvbarC',11111111,'2007-9-13' union all
select 1,'&brvbarA',12344455,'2007-10-10' union all
select 2,'&brvbarB',34223424,'2007-10-12'

select a.* from T a
inner join 
(
select 类型,max(发表时间)as 发表时间 from T group by 类型 
)b on a.类型=b.类型 and a.发表时间=b.发表时间
order by a.类型

--result:
1 &brvbarA 12344455 2007-10-10 00:00:00.000
2 &brvbarB 34223424 2007-10-12 00:00:00.000
3 &brvbarC 11111111 2007-09-13 00:00:00.000

------解决方案--------------------
SQL code
select * from 表 a where 发表时间=(select top 1 发表时间 from 表 where 类型=a.类型 order by 发表时间)
------解决方案--------------------
create table tb (id int ,lx char(2), lr char(20),fbsj datetime)
insert tb select 1 ,'A', '12344455', '2007-9-10' union all select 
2,'B', '34223424', '2007-9-12' union all select 
3,'C', '11111111', '2007-9-13' union all select 
1,'A', '12344455', '2007-10-10' union all select 
2,'B', '34223424', '2007-10-12' 
//最新的一条记录.
select * from tb a where not exists (select * from tb where id=a.id and fbsj>a.fbsj )
//最早的一条记录
select * from tb a where not exists (select * from tb where id=a.id and fbsj<a.fbsj )
------解决方案--------------------
declare @T table(id int,type varchar(8),text int,sdate datetime)
insert @T
select 1,'&brvbarA',12344455,'2007-9-10' union all
select 2,'&brvbarB',34223424,'2007-9-12' union all
select 3,'&brvbarC',11111111,'2007-9-13' union all
select 1,'&brvbarA',12344455,'2007-10-10' union all
select 2,'&brvbarB',34223424,'2007-10-12'


select id,type,text,max(sdate) sdate from (select id,type,text,sdate from @t group by type,id,text,sdate) a group by id,type,text
  相关解决方案