27,582
社区成员




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的时候把时间段也加上 谢谢
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 行受影响)
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 行受影响)
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 行受影响)