表tb中有数据 inout中I和O要一一对应起来,如下 cardnum dateshow timeshow inout
60010026 2011-4-2 07:34:50 I
60010026 2011-4-2 07:34:55 I
60010026 2011-4-2 10:40:23 O
60010026 2011-4-2 14:53:09 I
60010026 2011-4-2 17:55:30 O
60010037 2011-4-2 07:40:40 I
60010037 2011-4-2 10:35:20 O
60010037 2011-4-2 10:40:20 O
60010037 2011-4-2 10:46:09 I
60010037 2011-4-2 16:04:05 O
60010037 2011-4-2 16:21:45 I
60010037 2011-4-2 19:19:44 O想得到
60010026 2011-4-2 07:34:50
60010026 2011-4-2 07:34:55 10:40:23
60010026 2011-4-2 14:53:09 17:55:30
60010037 2011-4-2 07:40:40 10:35:20
60010037 2011-4-2 10:40:20
60010037 2011-4-2 10:46:09 16:04:05
60010037 2011-4-2 16:21:45 19:19:44
得怎么写select语句呢?
60010026 2011-4-2 07:34:50 I
60010026 2011-4-2 07:34:55 I
60010026 2011-4-2 10:40:23 O
60010026 2011-4-2 14:53:09 I
60010026 2011-4-2 17:55:30 O
60010037 2011-4-2 07:40:40 I
60010037 2011-4-2 10:35:20 O
60010037 2011-4-2 10:40:20 O
60010037 2011-4-2 10:46:09 I
60010037 2011-4-2 16:04:05 O
60010037 2011-4-2 16:21:45 I
60010037 2011-4-2 19:19:44 O想得到
60010026 2011-4-2 07:34:50
60010026 2011-4-2 07:34:55 10:40:23
60010026 2011-4-2 14:53:09 17:55:30
60010037 2011-4-2 07:40:40 10:35:20
60010037 2011-4-2 10:40:20
60010037 2011-4-2 10:46:09 16:04:05
60010037 2011-4-2 16:21:45 19:19:44
得怎么写select语句呢?
解决方案 »
- 跪求一条查询语句
- xp中安装SQL Server 2000要安装什么补丁?
- 问一下这个问题
- exec sp_executesql @s, N'@dt_begin datetime, @dt_end datetime', @dt_begin , @dt_end 什么意思?
- 我要哭了,win2003+sp2 安装SQL2000+sp4 客户端查询连接不上
- SQL2000 总是每天出现一两次自己停止,论坛上的有出现同类的我看了,没解决,急须求助
- sql 排序超难
- 救命啊!!这个SQL语句怎么写啊?
- 超级老火的问题!!!老鸟们都进来!!
- 有关时间戳的问题,请帮忙!
- sql rowlock WITH (tablockx)
- 嵌套查询的几个问题
timeshow2=(select top 1 timeshow from tb cardnum=a.cardnum and
timeshow>a.timeshow and inout='O')
from tb a where inout='I'
select '60010026' as cardnum,'2011-4-2' as dateshow,'07:34:50' as timeshow,'I' as inout union all
select '60010026','2011-4-2','07:34:55','I' union all
select '60010026','2011-4-2','10:40:23','O' union all
select '60010026','2011-4-2','14:53:09','I' union all
select '60010026','2011-4-2','17:55:30','O' union all
select '60010037','2011-4-2','07:40:40','I' union all
select '60010037','2011-4-2','10:35:20','O' union all
select '60010037','2011-4-2','10:40:20','O' union all
select '60010037','2011-4-2','10:46:09','I' union all
select '60010037','2011-4-2','16:04:05','O' union all
select '60010037','2011-4-2','16:21:45','I' union all
select '60010037','2011-4-2','19:19:44','O'),
t1 as (
select *,row_number()over (order by cardnum,dateshow,timeshow asc) as rn from t),
t2 as (
select cardnum,dateshow,
case inout
when 'I' then timeshow else null end as timein,
(select tb.timeshow from t1 tb where tb.rn=a.rn+1 and tb.inout='O')as timeout
from t1 a)
select * from t2 where timein is not null or timeout is not null
(
cardnum VARCHAR(10),
dateshow VARCHAR(10),
timeshow VARCHAR(10),
inout CHAR(1)
)
INSERT #temp
select '60010026', '2011-4-2', '07:34:50', 'I' union all
select '60010026', '2011-4-2', '07:34:55', 'I' union all
select '60010026', '2011-4-2', '10:40:23', 'O' union all
select '60010026', '2011-4-2', '14:53:09', 'I' union all
select '60010026', '2011-4-2', '17:55:30', 'O' union all
select '60010037', '2011-4-2', '07:40:40', 'I' union all
select '60010037', '2011-4-2', '10:35:20', 'O' union all
select '60010037', '2011-4-2', '10:40:20', 'O' union all
select '60010037', '2011-4-2', '10:46:09', 'I' union all
select '60010037', '2011-4-2', '16:04:05', 'O' union all
select '60010037', '2011-4-2', '16:21:45', 'I' union all
select '60010037', '2011-4-2', '19:19:44', 'O'
GO
--SQL:
;WITH cte AS
(
SELECT rowno = ROW_NUMBER() OVER(ORDER BY cardnum, dateshow, timeshow), * FROM #temp
)
SELECT A.rowno, A.cardnum, A.dateshow, Itimeshow = A.timeshow, Otimeshow = B.timeshow FROM
(SELECT * FROM cte WHERE inout = 'I') A
OUTER APPLY
(SELECT timeshow FROM cte WHERE inout = 'O' AND rowno = A.rowno + 1) B
UNION ALL
SELECT rowno, cardnum, dateshow, NULL, timeshow FROM cte T
WHERE inout = 'O'
AND NOT EXISTS(SELECT 1 FROM cte WHERE inout = 'I' AND rowno = T.rowno - 1)
ORDER BY rowno
/*
1 60010026 2011-4-2 07:34:50 NULL
2 60010026 2011-4-2 07:34:55 10:40:23
4 60010026 2011-4-2 14:53:09 17:55:30
6 60010037 2011-4-2 07:40:40 10:35:20
8 60010037 2011-4-2 NULL 10:40:20
9 60010037 2011-4-2 10:46:09 16:04:05
11 60010037 2011-4-2 16:21:45 19:19:44
*/
偶用的是2000
CREATE TABLE #temp
(
cardnum VARCHAR(10),
dateshow VARCHAR(10),
timeshow VARCHAR(10),
inout CHAR(1)
)
INSERT #temp
select '60010026', '2011-4-2', '07:34:50', 'I' union all
select '60010026', '2011-4-2', '07:34:55', 'I' union all
select '60010026', '2011-4-2', '10:40:23', 'O' union all
select '60010026', '2011-4-2', '14:53:09', 'I' union all
select '60010026', '2011-4-2', '17:55:30', 'O' union all
select '60010037', '2011-4-2', '07:40:40', 'I' union all
select '60010037', '2011-4-2', '10:35:20', 'O' union all
select '60010037', '2011-4-2', '10:40:20', 'O' union all
select '60010037', '2011-4-2', '10:46:09', 'I' union all
select '60010037', '2011-4-2', '16:04:05', 'O' union all
select '60010037', '2011-4-2', '16:21:45', 'I' union all
select '60010037', '2011-4-2', '19:19:44', 'O'
GOselect cardnum,dateshow,timeshow as timeshow1,
timeshow2=(select top 1 timeshow from #temp where cardnum = a.cardnum and
timeshow>a.timeshow and inout='O')
from #temp a where inout='I'drop table #temp/*cardnum dateshow timeshow1 timeshow2
---------- ---------- ---------- ----------
60010026 2011-4-2 07:34:50 10:40:23
60010026 2011-4-2 07:34:55 10:40:23
60010026 2011-4-2 14:53:09 17:55:30
60010037 2011-4-2 07:40:40 10:35:20
60010037 2011-4-2 10:46:09 16:04:05
60010037 2011-4-2 16:21:45 19:19:44(6 行受影响)
CREATE TABLE #temp
(
cardnum VARCHAR(10),
dateshow VARCHAR(10),
timeshow VARCHAR(10),
inout CHAR(1)
)
INSERT #temp
select '60010026', '2011-4-2', '07:34:50', 'I' union all
select '60010026', '2011-4-2', '07:34:55', 'I' union all
select '60010026', '2011-4-2', '10:40:23', 'O' union all
select '60010026', '2011-4-2', '14:53:09', 'I' union all
select '60010026', '2011-4-2', '17:55:30', 'O' union all
select '60010037', '2011-4-2', '07:40:40', 'I' union all
select '60010037', '2011-4-2', '10:35:20', 'O' union all
select '60010037', '2011-4-2', '10:40:20', 'O' union all
select '60010037', '2011-4-2', '10:46:09', 'I' union all
select '60010037', '2011-4-2', '16:04:05', 'O' union all
select '60010037', '2011-4-2', '16:21:45', 'I' union all
select '60010037', '2011-4-2', '19:19:44', 'O'
GOselect *,rn = identity(int,1,1)
into #tb
from #tempselect isnull(a.cardnum,b.cardnum) cardnum,isnull(a.dateshow,b.dateshow) dateshow,
a.timeshow as timeshow1,b.timeshow as timeshow2
from (select * from #tb where inout = N'I') a
full join (select * from #tb where inout = N'O') b on a.rn + 1 = b.rn
order by isnull(a.rn,b.rn)drop table #temp,#tb/*cardnum dateshow timeshow1 timeshow2
---------- ---------- ---------- ----------
60010026 2011-4-2 07:34:50 NULL
60010026 2011-4-2 07:34:55 10:40:23
60010026 2011-4-2 14:53:09 17:55:30
60010037 2011-4-2 07:40:40 10:35:20
60010037 2011-4-2 NULL 10:40:20
60010037 2011-4-2 10:46:09 16:04:05
60010037 2011-4-2 16:21:45 19:19:44(7 行受影响)