谢谢!谢谢!可能大牛人都上班去了还有个疑问就是 SQL查询到底能不能实现这种功能 ?我是一只十足的菜鸟! 唉! 比较郁闷!
解决方案 »
- 非常怪异的SQL现象! 求高手给分析1下原因,我都糊涂了!
- 根据指定的年和月查询数据
- order 表中添加数据
- 请问这个SQL查询语句应该怎么写,请高手指点!
- 关于sql server中Varchar长度的问题!
- 查不出来记录 多人请教
- 请高手帮助诊断以下语句,急,高分相送!!!!
- 机器上已经安装SQL Sever2005,请问在机器上再安装Oracle9i或Oracle10会不会有冲突?
- 高分求教简单的SQL语句,顶者有分!
- vfp中如何得到当前工程所在的路径?
- 获得某个字段中最大奇数(偶数)所在的记录集,SQL语句应该如何写???
- 我有一个字段是varchar,select 字段的时候有没有办法转化为字节?
SELECT * FROM CSDN WHERE User_Name like '%Lonely_Vane%'
--查询结果:
--路过、、、、
insert into t1 select 1,'2009-01-01','a'
insert into t1 select 2,'2009-01-02','b'
insert into t1 select 3,'2009-01-03','c' create table t2(变更时间 datetime,原拥有人 varchar(10),现拥有人 varchar(10),父ID int)
insert into t2 select '2009-02-01','a','a1',1
insert into t2 select '2009-03-01','a1','a',1
insert into t2 select '2009-04-01','a','a1',1
go
create function f_getInfo(@ID int)
returns @t table(ID int identity(1,1),起始时间 datetime,结束时间 datetime,原拥有人 varchar(10),现拥有人 varchar(10))
as
begin
insert into @t(起始时间,结束时间,原拥有人,现拥有人)
select
top 1 a.创建时间,b.变更时间,a.拥有人,b.现拥有人
from
t1 a
left join
t2 b
on
a.ID=b.父ID and a.拥有人=b.原拥有人 and a.创建时间<b.变更时间
where
a.ID=@ID
order by
b.变更时间
while @@rowcount>0
begin
insert into @t(起始时间,结束时间,原拥有人,现拥有人)
select
a.结束时间,b.变更时间,a.现拥有人,b.现拥有人
from
(select top 1 * from @t order by ID desc) a
left join
t2 b
on
a.现拥有人=b.原拥有人 and b.父ID=@ID and b.变更时间>a.结束时间
where
a.现拥有人 is not null
order by
b.变更时间
end return
end
goselect 起始时间,结束时间,原拥有人 from dbo.f_getInfo(1)
/*
起始时间 结束时间 原拥有人
------------------------------------------------------ ------------------------------------------------------ ----------
2009-01-01 00:00:00.000 2009-02-01 00:00:00.000 a
2009-02-01 00:00:00.000 2009-03-01 00:00:00.000 a1
2009-03-01 00:00:00.000 2009-04-01 00:00:00.000 a
2009-04-01 00:00:00.000 NULL a1
*/
godrop function f_getInfo
drop table t1,t2
go