表A 列a1=事件内容 a2部门 a3记录时间 a4标示列(guid)
查账 会计 2008-5-17 19:47:29 guid-1
人员调动 会计 2008-3-28 09:00:00 guid-2
休假3天 会计 2008-4-30 19:00:00 guid-3
人员调用 人事 2007-6-2 13:12:02 guid-4
招聘 人事 2008-3-2 9:30:00 guid-5
采购原材料 采购 2008-3-13 09:00:00 guid-6
现在是要求: 用一个sql语句select之后获得一张表B,表B要求按照列"a2部门"获得每个部门"a3最新时间"的"a1事件内容"
举例里面有三个部分,其中会计3个事件,人事2个事件
在B表会有3个记录
分别会计1,人事1,采购1,都是各个部门的最新时间
希望各位高手帮忙
以上是昨天发帖求助的内容,后来经过网友们的热心帮助,现在得到以下两个方法
两种方法
方法一:
select aaa.a1,aaa.a2,aaa.a3 from test aaa
inner join (select max(a3) as a3,a2 from test group by a2) bbb
on aaa.a2=bbb.a2 where aaa.a3=bbb.a3 方法二:
select * from test T
where not exists(select 1 from test where a2 = T.a2 and a3 > T.a3 )
在实际应用中发现一个问题,那就是当一个部门有2个事件具有相同时间的时候,返回的值就不再是唯一的我需要的是"获知每个部分的最新事件",只要最新(唯一),不要多结果
查账 会计 2008-5-17 19:47:29 guid-1
人员调动 会计 2008-3-28 09:00:00 guid-2
休假3天 会计 2008-4-30 19:00:00 guid-3
人员调用 人事 2007-6-2 13:12:02 guid-4
招聘 人事 2008-3-2 9:30:00 guid-5
采购原材料 采购 2008-3-13 09:00:00 guid-6
现在是要求: 用一个sql语句select之后获得一张表B,表B要求按照列"a2部门"获得每个部门"a3最新时间"的"a1事件内容"
举例里面有三个部分,其中会计3个事件,人事2个事件
在B表会有3个记录
分别会计1,人事1,采购1,都是各个部门的最新时间
希望各位高手帮忙
以上是昨天发帖求助的内容,后来经过网友们的热心帮助,现在得到以下两个方法
两种方法
方法一:
select aaa.a1,aaa.a2,aaa.a3 from test aaa
inner join (select max(a3) as a3,a2 from test group by a2) bbb
on aaa.a2=bbb.a2 where aaa.a3=bbb.a3 方法二:
select * from test T
where not exists(select 1 from test where a2 = T.a2 and a3 > T.a3 )
在实际应用中发现一个问题,那就是当一个部门有2个事件具有相同时间的时候,返回的值就不再是唯一的我需要的是"获知每个部分的最新事件",只要最新(唯一),不要多结果
--try:select * from test T
where not exists(select 1 from test where a2 = T.a2 and a3 > T.a3
and substring(a4,charindex('-',a4)+1,len(a4))>substring(a.a4,charindex('-',a.a4)+1,len(a.a4)))
是相同的吗?如果相同
可以用select distinct * from test T
where 1>(select count(1) from test where a2 = T.a2 and a3 > T.a3 )
declare @b table(field1 varchar(20),field2 varchar(10),field3 datetime,field4 varchar(10))
insert @b
select 'A1','A','2008-01-01','10'
union all
select 'A1','A','2008-02-01','20'
union all
select 'A2','A','2008-02-01','30'union all
select 'B1','B','2008-01-01','10'
union all
select 'B2','B','2008-02-01','20'
union all
select 'B3','B','2008-03-01','30' union all
select 'C1','C','2008-01-01','10'
union all
select 'C2','C','2008-02-01','20'
union all
select 'C3','C','2008-02-01','30'select px= identity(int,1,1),* into # from
(select top 100 percent * from @b order by field2,field3 desc) aselect b.field1,b.field2,a.field3,b.field4
from
(select field2,max(field3) as field3 from # group by field2) a
right join
(
select *
from # a
where not exists(select 1 from #
where field2 = a.field2 and px < a.px )) b
on a.field2 = b.field2 drop table #
field1 field2 field3 field4
-------------------- ---------- ------------------------------------------------------ ----------
A1 A 2008-02-01 00:00:00.000 20
B3 B 2008-03-01 00:00:00.000 30
C2 C 2008-02-01 00:00:00.000 20
这是石头大侠教的
if object_id('tempdb.dbo.#tb') is not null drop table #tb
create table #tb (a int, b int, c int)--a,b,c相当于a2,a3,a4
insert into #tb values(1,1,1)
insert into #tb values(1,2,2)--1为理想的数据
insert into #tb values(2,1,3)
insert into #tb values(2,2,4)
insert into #tb values(2,2,5)--2和3是LZ说的情况,c字段相当于a4标示列(guid)
insert into #tb values(3,1,1)
insert into #tb values(3,2,1)
insert into #tb values(3,2,1)select * from #tb t
where not exists
(select 1 from #tb where a=t.a and b>t.b)--最初的语句select * from #tb t
where not exists
(select 1 from #tb where a=t.a and b>t.b and c>t.c)--不能处理2的情况select distinct * from #tb t
where not exists
(select 1 from #tb where a=t.a and b>t.b) --可以处理3的情况select * from (
select distinct * from #tb t
where not exists
(select 1 from #tb where a=t.a and b>t.b)) x
where not exists(
select 1 from
(select distinct * from #tb t
where not exists
(select 1 from #tb where a=t.a and b>t.b)) y
where x.a=y.a and x.b=y.b and x.c<y.c)--麻烦了点,但是可以处理2的情况