求一存储过程

Mark杨 2010-09-03 04:22:42

declare @tb table(ID int,版号 nvarchar(10),时间 datetime,行为 nvarchar(4),操作者 nvarchar(10))

insert @tb
select 1,'KY10001','2010-09-02 10:25:56.153','停','b' union all
select 2,'KY10005','2010-09-02 19:05:27.983','开','a' union all
select 3,'KY10003','2010-09-03 08:16:04.187','开','a' union all
select 4,'KY10003','2010-09-03 16:32:32.060','停','c' union all
select 5,'KY10005','2010-09-03 17:06:34.390','停','d' union all
select 6,'KY10003','2010-09-03 18:56:56.153','开','a' union all
select 7,'KY10006','2010-09-03 19:23:23.120','开','a'

select * from @tb
-->插入数据显示(莫一时间段内的数据)
ID 版号 时间 行为 操作者
----------- ---------- ------------------------------------------------------ ---- ----------
1 KY10001 2010-09-02 10:25:56.153 停 b
2 KY10005 2010-09-02 19:05:27.983 开 a
3 KY10003 2010-09-03 08:16:04.187 开 a
4 KY10003 2010-09-03 16:32:32.060 停 c
5 KY10005 2010-09-03 17:06:34.390 停 d
6 KY10003 2010-09-03 18:56:56.153 开 a
7 KY10006 2010-09-03 19:23:23.120 开 a


-->我想要的结果
版号 开时间 停时间 操作者
---------- -------------------------- ---------------------------- -------
KY10001 2010-09-02 10:25:56.153 b
KY10005 2010-09-02 19:05:27.983 2010-09-03 17:06:34.390 c
KY10003 2010-09-03 08:16:04.187 2010-09-03 16:32:32.060 d
KY10003 2010-09-03 18:56:56.153
KY10006 2010-09-03 19:23:23.120

操作者显示 停 的那条记录的操作者,没有停的 不显示或显示开的操作者

写sql的时候把时间段也加上 谢谢
...全文
90 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
天下如山 2010-09-03
  • 打赏
  • 举报
回复
学习 !
华夏小卒 2010-09-03
  • 打赏
  • 举报
回复
 select 版号,
开时间=case when 行为='开' then 时间 else (select max(时间) from @tb where 版号=t.版号 and 时间<t.时间 and 行为='开' -- and 时间 betwenn @s and @e
) end,
停时间=case when 行为='停' then 时间 else (select min(时间) from @tb where 版号=t.版号 and 时间>t.时间 and 行为='停' -- and 时间 betwenn @s and @e
) end,
操作者=case when 行为='停' then 操作者 else '' end
from @tb t
where 行为='停'
or not exists (select 1 from @tb where 版号=t.版号 and 时间>t.时间 and 行为='停' -- and 时间 betwenn @s and @e
)
--and 时间 betwenn @s and @e




版号 开时间 停时间 操作者
---------- ----------------------- ----------------------- ----------
KY10001 NULL 2010-09-02 10:25:56.153 b
KY10003 2010-09-03 08:16:04.187 2010-09-03 16:32:32.060 c
KY10005 2010-09-02 19:05:27.983 2010-09-03 17:06:34.390 d
KY10003 2010-09-03 18:56:56.153 NULL
KY10006 2010-09-03 19:23:23.120 NULL

(5 行受影响)
Mark杨 2010-09-03
  • 打赏
  • 举报
回复
解决 谢谢了
华夏小卒 2010-09-03
  • 打赏
  • 举报
回复
declare @tb table(ID int,版号 nvarchar(10),时间 datetime,行为 nvarchar(4),操作者 nvarchar(10))

insert @tb
select 1,'KY10001','2010-09-02 10:25:56.153','停','b' union all
select 2,'KY10005','2010-09-02 19:05:27.983','开','a' union all
select 3,'KY10003','2010-09-03 08:16:04.187','开','a' union all
select 4,'KY10003','2010-09-03 16:32:32.060','停','c' union all
select 5,'KY10005','2010-09-03 17:06:34.390','停','d' union all
select 6,'KY10003','2010-09-03 18:56:56.153','开','a' union all
select 7,'KY10006','2010-09-03 19:23:23.120','开','a'

declare @s datetime,@e datetime

select 版号,
开时间=(select max(时间) from @tb where 版号=t.版号 and 时间<t.时间 and 行为='开' -- and 时间 betwenn @s and @e
),
停时间=时间,
操作者
from @tb t
where 行为='停'
--and 时间 betwenn @s and @e
union all
select 版号,
开时间=时间,停时间=null,操作者=''
from @tb m
where 行为='开'
and not exists (select 1 from @tb where 版号=m.版号 and 时间>m.时间 and 行为='停' -- and 时间 betwenn @s and @e
)
-- and 时间 betwenn @s and @e


版号 开时间 停时间 操作者
---------- ----------------------- ----------------------- ----------
KY10001 NULL 2010-09-02 10:25:56.153 b
KY10003 2010-09-03 08:16:04.187 2010-09-03 16:32:32.060 c
KY10005 2010-09-02 19:05:27.983 2010-09-03 17:06:34.390 d
KY10003 2010-09-03 18:56:56.153 NULL
KY10006 2010-09-03 19:23:23.120 NULL

(5 行受影响)
Rotel-刘志东 2010-09-03
  • 打赏
  • 举报
回复
case when
华夏小卒 2010-09-03
  • 打赏
  • 举报
回复
declare @tb table(ID int,版号 nvarchar(10),时间 datetime,行为 nvarchar(4),操作者 nvarchar(10))

insert @tb
select 1,'KY10001','2010-09-02 10:25:56.153','停','b' union all
select 2,'KY10005','2010-09-02 19:05:27.983','开','a' union all
select 3,'KY10003','2010-09-03 08:16:04.187','开','a' union all
select 4,'KY10003','2010-09-03 16:32:32.060','停','c' union all
select 5,'KY10005','2010-09-03 17:06:34.390','停','d' union all
select 6,'KY10003','2010-09-03 18:56:56.153','开','a' union all
select 7,'KY10006','2010-09-03 19:23:23.120','开','a'

declare @s datetime,@e datetime

select 版号,
开时间=(select max(时间) from @tb where 版号=t.版号 and 时间<t.时间 and 行为='开' -- and 时间 betwenn @s and @e
),
停时间=时间,
操作者
from @tb t
where 行为='停'
--and 时间 betwenn @s and @e


版号 开时间 停时间 操作者
---------- ----------------------- ----------------------- ----------
KY10001 NULL 2010-09-02 10:25:56.153 b
KY10003 2010-09-03 08:16:04.187 2010-09-03 16:32:32.060 c
KY10005 2010-09-02 19:05:27.983 2010-09-03 17:06:34.390 d

(3 行受影响)
Mark杨 2010-09-03
  • 打赏
  • 举报
回复
虽然一个没有开 一个没有停
但是 只要在指定的时间段内 就都要显示
华夏小卒 2010-09-03
  • 打赏
  • 举报
回复
KY10003 2010-09-03 18:56:56.153
KY10006 2010-09-03 19:23:23.120 这2句为什么显示
大家这么喜欢,我就不要资源分了,改成0分了,我为我一己私利感到抱歉…… MongoDB学习手册 ......................................................................................................................... 1 说明.......................................................................................................................................... 2 一、 Mongodb简介 .......................................................................................................... 4 二、 MongoDB特性 ......................................................................................................... 5 适用场景: ....................................................................................................................... 5 不适用场景: ................................................................................................................... 6 三、 MongoDB的工作方式.............................................................................................. 6 四、 MongoDB的下载 ..................................................................................................... 8 五、 MongoDB的安装 ..................................................................................................... 9 六、 MongoDB数据类型................................................................................................ 12 1. Timestamp类型 ...................................................................................................... 12 2. ObjectId类型 .......................................................................................................... 12 3. 数据库关联 ............................................................................................................. 14 七、 GridFS文件系统 ..................................................................................................... 15 八、 索引 ......................................................................................................................... 16 九、 主(Master)/从(slave)数据库同步 ................................................................ 20 1. 建立主/从服务器 ................................................................................................... 20 2. 相关参数说明 ......................................................................................................... 21 3. Slave顶替Master ................................................................................................... 22 4. 切换Master/Slave角色 ......................................................................................... 22 5. 更新主服务器位置 ................................................................................................. 22 十、 MongoDB分片和集群............................................................................................ 24 1. 简单分片实例 ......................................................................................................... 24 2. 高级分片实例 ......................................................................................................... 29 十一、 数据库基本操作:增查删改 ............................................................................. 37 1. Insert ....................................................................................................................... 37 2. Query ....................................................................................................................... 38 3. Remove.................................................................................................................... 52 4. Update ..................................................................................................................... 53 十二、 Shell控制台 ........................................................................................................ 62 1. 执行.js文件 ............................................................................................................ 62 2. –eval ........................................................................................................................ 62 3. 脚本和互动的区别 ................................................................................................. 62 十三、 安全与认证 ......................................................................................................... 63 1) 开启安全认证 ......................................................................................................... 63 2) 添加用户 ................................................................................................................. 64 3) 认证 ......................................................................................................................... 64

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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