求一存储过程

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的时候把时间段也加上 谢谢
...全文
83 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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句为什么显示

27,582

社区成员

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

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