有表A如下id emp_id name kind update_time
1 1 test1 1 2011-07-25 08:45:14.921
2 1 test2 1 2011-07-26 08:46:14.921
3 1 test3 2 2011-07-27 08:47:14.921
4 2 test4 3 2011-07-28 08:48:14.921
5 2 test5 3 2011-07-29 08:49:14.921 想查询 emp_id = 1 每两条记录中间可以插入一条。现在emp_id=1 可以得到5条记录 ,两条之间的数据kind设置为5.如下
emp_id name kind update_time
1 test1 1 2011-07-25 08:45:14.921
1 test1 5 2011-07-25 08:45:14.921 ---》两条之间插入 kind 为5 时间为上条记录时间
1 test2 1 2011-07-26 08:46:14.921
1 test2 1 2011-07-26 08:46:14.921 ---》两条之间插入 kind 为5 时间为上条记录时间
1 test3 2 2011-07-27 08:47:14.921 emp_id = 2 每两条记录中间可以插入一条。现在emp_id=2 可以得到3条记录 ,两条之间的数据kind设置为5.如下
emp_id name kind update_time
2 test4 3 2011-07-28 08:48:14.921
2 test4 5 2011-07-28 08:48:14.921 ---》两条之间插入 kind 为5 时间为上条记录时间
2 test5 3 2011-07-29 08:49:14.921 最终想要的结果为
emp_id name kind update_time
1 test1 1 2011-07-25 08:45:14.921
1 test1 5 2011-07-25 08:45:14.921 ---》两条之间插入 kind 为5 时间为上条记录时间
1 test2 1 2011-07-26 08:46:14.921
1 test2 1 2011-07-26 08:46:14.921 ---》两条之间插入 kind 为5 时间为上条记录时间
1 test3 2 2011-07-27 08:47:14.921 2 test4 3 2011-07-28 08:48:14.921
2 test4 5 2011-07-28 08:48:14.921 ---》两条之间插入 kind 为5 时间为上条记录时间
2 test5 3 2011-07-29 08:49:14.921 最终查询想要结果为8条。表中5条,追加3条。sql文该怎么写呢。请高人指点
1 1 test1 1 2011-07-25 08:45:14.921
2 1 test2 1 2011-07-26 08:46:14.921
3 1 test3 2 2011-07-27 08:47:14.921
4 2 test4 3 2011-07-28 08:48:14.921
5 2 test5 3 2011-07-29 08:49:14.921 想查询 emp_id = 1 每两条记录中间可以插入一条。现在emp_id=1 可以得到5条记录 ,两条之间的数据kind设置为5.如下
emp_id name kind update_time
1 test1 1 2011-07-25 08:45:14.921
1 test1 5 2011-07-25 08:45:14.921 ---》两条之间插入 kind 为5 时间为上条记录时间
1 test2 1 2011-07-26 08:46:14.921
1 test2 1 2011-07-26 08:46:14.921 ---》两条之间插入 kind 为5 时间为上条记录时间
1 test3 2 2011-07-27 08:47:14.921 emp_id = 2 每两条记录中间可以插入一条。现在emp_id=2 可以得到3条记录 ,两条之间的数据kind设置为5.如下
emp_id name kind update_time
2 test4 3 2011-07-28 08:48:14.921
2 test4 5 2011-07-28 08:48:14.921 ---》两条之间插入 kind 为5 时间为上条记录时间
2 test5 3 2011-07-29 08:49:14.921 最终想要的结果为
emp_id name kind update_time
1 test1 1 2011-07-25 08:45:14.921
1 test1 5 2011-07-25 08:45:14.921 ---》两条之间插入 kind 为5 时间为上条记录时间
1 test2 1 2011-07-26 08:46:14.921
1 test2 1 2011-07-26 08:46:14.921 ---》两条之间插入 kind 为5 时间为上条记录时间
1 test3 2 2011-07-27 08:47:14.921 2 test4 3 2011-07-28 08:48:14.921
2 test4 5 2011-07-28 08:48:14.921 ---》两条之间插入 kind 为5 时间为上条记录时间
2 test5 3 2011-07-29 08:49:14.921 最终查询想要结果为8条。表中5条,追加3条。sql文该怎么写呢。请高人指点
解决方案 »
- 给大家拜年,顺便放个烟花
- 求高手指导 通过用户注册得到的值修改不了,但是直接输入到数据库里的值却可以更改
- jsp上传下载.急...
- 大家做项目是用的struts1还是struts2呀
- 如何在jsp页面 用ognl 访问object数组
- JSP session问题大家帮忙,在下是菜鸟如有不对的地方请谅解
- 今我感叹的JB2007,可惜了……
- 一个预编译语句的错误!
- 网页分享的问题
- linux内存使用量过大,ps线程显示-Djava.util.logging.config.file
- struts2 的简单登录项目--2
- JSP用MVC时提示bean bvo not found within scope
-->建立测试数据:
create table tb
(
id int,
emp_id int,
name varchar(10),
kind int,
update_time datetime
)
insert into tb
select 1 ,1 ,'test1' ,1 ,'2011-07-25 08:45:14.921' union all
select 2 ,1 ,'test2' ,1 ,'2011-07-26 08:46:14.921' union all
select 3 ,1 ,'test3' ,2 ,'2011-07-27 08:47:14.921' union all
select 4 ,2 ,'test4' ,3 ,'2011-07-28 08:48:14.921' union all
select 5 ,2 ,'test5' ,3 ,'2011-07-29 08:49:14.921'
go;with ach as
(
select *,rid=row_number() over (partition by emp_id order by id)
from tb
)select emp_id,name,kind,update_time
from tb
union all
select b.emp_id,b.name,5,b.update_time
from master..spt_values a,ach b
where number = b.rid and a.[type] = 'p'
and number > 0 and number <= (select max(rid) from ach where emp_id = b.emp_id)-1
order by emp_id,name,kinddrop table tb/****************emp_id name kind update_time
----------- ---------- ----------- -----------------------
1 test1 1 2011-07-25 08:45:14.920
1 test1 5 2011-07-25 08:45:14.920
1 test2 1 2011-07-26 08:46:14.920
1 test2 5 2011-07-26 08:46:14.920
1 test3 2 2011-07-27 08:47:14.920
2 test4 3 2011-07-28 08:48:14.920
2 test4 5 2011-07-28 08:48:14.920
2 test5 3 2011-07-29 08:49:14.920(8 行受影响)
GO
if not object_ID('Tempdb..tmp') is null
drop table tmp
Go
Create table tmp([id] int,[emp_id] int,[name] nvarchar(5),[kind] int,[update_time] nvarchar(50))
Insert tmp
select 1,1,'test1',1,'2011-07-25 08:45:14.921' union all
select 2,1,'test2',1,'2011-07-26 08:46:14.921' union all
select 3,1,'test3',2,'2011-07-27 08:47:14.921' union all
select 4,2,'test4',3,'2011-07-28 08:48:14.921' union all
select 5,2,'test5',3,'2011-07-29 08:49:14.921'
GoSelect emp_id,name,kind,update_time
from tmp As a
Union All
Select emp_id,name,5,update_time
From tmp As a
Where Exists(Select 1 From tmp Where emp_id=a.emp_id And id>a.ID)
Order By emp_id,update_time,kind /*Order By 部份需要根據實際的情況去排序*/
/*
-----------------------------------------
1 test1 1 2011-07-25 08:45:14.921
1 test1 5 2011-07-25 08:45:14.921
1 test2 1 2011-07-26 08:46:14.921
1 test2 5 2011-07-26 08:46:14.921
1 test3 2 2011-07-27 08:47:14.921
2 test4 3 2011-07-28 08:48:14.921
2 test4 5 2011-07-28 08:48:14.921
2 test5 3 2011-07-29 08:49:14.921
*/
(
id int,
emp_id int,
name varchar(10),
kind int,
update_time datetime
)
insert into tb
select 1 ,1 ,'test1' ,1 ,'2011-07-25 08:45:14.921' union all
select 2 ,1 ,'test2' ,1 ,'2011-07-26 08:46:14.921' union all
select 3 ,1 ,'test3' ,2 ,'2011-07-27 08:47:14.921' union all
select 4 ,2 ,'test4' ,3 ,'2011-07-28 08:48:14.921' union all
select 5 ,2 ,'test5' ,3 ,'2011-07-29 08:49:14.921' insert tb
select id,emp_id,name,5,update_time
from tb a
where exists
(select 1 from tb b where b.emp_id=a.emp_id and b.id=a.id+1)select emp_id,name,kind,update_time from tb order by emp_id,name,kind/*
emp_id name kind update_time
----------- ---------- ----------- -----------------------
1 test1 1 2011-07-25 08:45:14.920
1 test1 5 2011-07-25 08:45:14.920
1 test2 1 2011-07-26 08:46:14.920
1 test2 5 2011-07-26 08:46:14.920
1 test3 2 2011-07-27 08:47:14.920
2 test4 3 2011-07-28 08:48:14.920
2 test4 5 2011-07-28 08:48:14.920
2 test5 3 2011-07-29 08:49:14.920
*/
from tb a
where exists
(select 1 from tb b where b.emp_id=a.emp_id and b.id=a.id+1)如果有检索条件tb b这个应该为 select id,emp_id,name,5,update_time from tb where 条件 as tt