新手上路。Delphi过滤数据库查询结果的问题。望指教

wjs496249880 2010-07-13 08:19:41
数据库表A存有如下类型的字段:
Time biaozhi 姓名
2010-07-08 12:34:56 1 s
2010-07-08 16:17:56 1 s
2010-07-09 11:13:13 2 s
2010-07-09 19:13:13 2 s
2010-07-10 11:13:13 2 s
2010-07-11 11:13:13 1 s
2010-07-12 14:13:13 1 s
biaozhi为一个标志字段。当标志从1变到2时,该Time字段的值为开始时间,当标志从2变到1时,Time字段的值为结束时间。
期望的查询结果为:
开始时间 结束时间 姓名
2010-07-09 11:13:13 2010-07-11 11:13:13 s

请问大侠,这该如何实现?是在SQL语句里实现呢,还是对查询的结果进行过滤判断呀?就是希望把结果显示到stringgrid上。SQL语句可以实现么?
各位大侠!指教!!!
...全文
286 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
wjs496249880 2010-08-13
  • 打赏
  • 举报
回复
希望josy大哥继续帮帮我。
wjs496249880 2010-07-13
  • 打赏
  • 举报
回复
谢谢josy大哥,也同时谢谢rmljoe、wufeng4552、xys_777。
谢谢你们的帮助。问题终于解决了。用的是josy大哥23#的方法。
结贴。
百年树人 2010-07-13
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 wjs496249880 的回复:]
高手们,大哥,帮帮我吧。josy大哥,在2000里还得用临时表获得行号么?
[/Quote]


--sql2000用临时表
select *,px=(select count(1)+1 from tb where 姓名=t.姓名 and [time]<t.[time])
into #1
from tb t
go

select a.[time],a.姓名,a.biaozhi,px=identity(int,1,1)
into #2
from #1 a,#1 b
where a.姓名=b.姓名 and a.px=b.px+1
and (a.biaozhi=1 and b.biaozhi=2 or a.biaozhi=2 and b.biaozhi=1)
go

select
开始时间= max(case when biaozhi=2 then [time] else '' end),
结束时间= max(case when biaozhi=1 then [time] else '' end),
姓名
from #2
group by 姓名,(px-1)/2

/**
开始时间 结束时间 姓名
----------------------- ----------------------- ----
2010-07-09 11:13:13.000 2010-07-11 11:13:13.000 s

(1 行受影响)

**/
wjs496249880 2010-07-13
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 xys_777 的回复:]
来蹭点分


SQL code
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([Time] datetime,[biaozhi] int,[姓名] varchar(1))
insert [tb]
select '2010-07-08 12:34:56',1,'s' ……
[/Quote]
xys_777 大哥的存储过程可以实现想要的结果。不晓得不用存储过程的话,用SQL查询语句能不能实现?
wjs496249880 2010-07-13
  • 打赏
  • 举报
回复
高手们,大哥,帮帮我吧。josy大哥,在2000里还得用临时表获得行号么?
永生天地 2010-07-13
  • 打赏
  • 举报
回复
来蹭点分

---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([Time] datetime,[biaozhi] int,[姓名] varchar(1))
insert [tb]
select '2010-07-08 12:34:56',1,'s' union all
select '2010-07-08 16:17:56',1,'s' union all
select '2010-07-09 11:13:13',2,'s' union all
select '2010-07-09 19:13:13',2,'s' union all
select '2010-07-10 11:13:13',2,'s' union all
select '2010-07-11 11:13:13',1,'s' union all
select '2010-07-12 14:13:13',1,'s'
go
---查询---
if object_id('p_test') is not null drop proc p_test
go
create proc p_test
as
begin
create table #t (id int identity,开始时间 datetime,结束时间 datetime,姓名 varchar(1) )
declare @time datetime,@biaozhi1 int,@biaozhi int,@姓名 varchar(1),@i int,@id int
declare cur cursor for select * from tb
open cur
fetch cur into @time,@biaozhi,@姓名
set @i=0
while @@fetch_status=0
begin
if @biaozhi1=1 and @biaozhi=2
begin
insert #t (开始时间,姓名) select @time,@姓名
select @id=scope_identity()
set @i=@i+1
end
if @biaozhi1 = 2 and @biaozhi=1
begin
update #t set 结束时间 = @time,姓名=@姓名 where id=@id
set @i=@i+1
end
if @i=2
set @i=0
set @biaozhi1=@biaozhi
fetch cur into @time,@biaozhi,@姓名
end
close cur
deallocate cur
select * from #t
end
go
p_test
/*
期望的查询结果为:

id 开始时间 结束时间 姓名
----------- ----------------------- ----------------------- ----
1 2010-07-09 11:13:13.000 2010-07-11 11:13:13.000 s

(1 行受影响)

*/
wjs496249880 2010-07-13
  • 打赏
  • 举报
回复
期待高手指教
wjs496249880 2010-07-13
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 josy 的回复:]
引用 15 楼 wjs496249880 的回复:
rmljoe和wufeng4552 大哥的SQL得到的都是一条记录,如果数据库中用很多条从1变到2,从2变到1的记录,如何将它们都查询出来啊。

试试

SQL code
---查询---
select
开始时间= max(case when biaozhi=2 then [time] else '' end),
结束时间= ……
[/Quote]
josy大哥。我用的是2000的数据库,不支持row_number() 函数啊。
百年树人 2010-07-13
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 wjs496249880 的回复:]
rmljoe和wufeng4552 大哥的SQL得到的都是一条记录,如果数据库中用很多条从1变到2,从2变到1的记录,如何将它们都查询出来啊。
[/Quote]
试试
---查询---
select
开始时间= max(case when biaozhi=2 then [time] else '' end),
结束时间= max(case when biaozhi=1 then [time] else '' end),
姓名
from(
select a.[time],a.姓名,a.biaozhi,rn=row_number() over(order by a.[time])
from
(select *,rn=row_number() over(partition by 姓名 order by [time])
from tb
) a,
(select *,rn=row_number() over(partition by 姓名 order by [time])
from tb
) b
where a.姓名=b.姓名 and a.rn=b.rn+1
and (a.biaozhi=1 and b.biaozhi=2 or a.biaozhi=2 and b.biaozhi=1)
) t
group by 姓名,(rn-1)/2

---结果---
开始时间 结束时间 姓名
----------------------- ----------------------- ----
2010-07-09 11:13:13.000 2010-07-11 11:13:13.000 s

(1 行受影响)
wjs496249880 2010-07-13
  • 打赏
  • 举报
回复
几位大哥!!!
wjs496249880 2010-07-13
  • 打赏
  • 举报
回复
rmljoe和wufeng4552 大哥的SQL得到的都是一条记录,如果数据库中用很多条从1变到2,从2变到1的记录,如何将它们都查询出来啊。
wjs496249880 2010-07-13
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 wufeng4552 的回复:]
引用 12 楼 wjs496249880 的回复:
引用 6 楼 rmljoe 的回复:
SQL code
create table #tb(Time datetime, biaozhi int ,姓名 varchar(20))
go
insert into #tb
select '2010-07-08 12:34:56','1','s'
union all select '2010……
[/Quote]
也是得到一条记录的。大哥
水族杰纶 2010-07-13
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wjs496249880 的回复:]
引用 6 楼 rmljoe 的回复:
SQL code
create table #tb(Time datetime, biaozhi int ,姓名 varchar(20))
go
insert into #tb
select '2010-07-08 12:34:56','1','s'
union all select '2010-07-08 16:17:56','1','s'
u……
[/Quote]
试下4#看看
@tb换成自己的表
wjs496249880 2010-07-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 rmljoe 的回复:]
SQL code
create table #tb(Time datetime, biaozhi int ,姓名 varchar(20))
go
insert into #tb
select '2010-07-08 12:34:56','1','s'
union all select '2010-07-08 16:17:56','1','s'
union all……
[/Quote]
大哥。用top 1不行吧,假如我下面还有很多这样的数据,很多条从1变到2或从2变到1,但用top 1只得到一条啊。
wjs496249880 2010-07-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 josy 的回复:]
SQL code
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([Time] datetime,[biaozhi] int,[姓名] varchar(1))
insert [tb]
select '2010-07-08 12:34:56',1,'s' union all
……
[/Quote]
大哥,SQL2000不支持row_number() 函数啊。
mm51221 2010-07-13
  • 打赏
  • 举报
回复
学习了 顶顶顶 ~~~
wjs496249880 2010-07-13
  • 打赏
  • 举报
回复
一下子来了这么多高手,我先试下啊。
百年树人 2010-07-13
  • 打赏
  • 举报
回复
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([Time] datetime,[biaozhi] int,[姓名] varchar(1))
insert [tb]
select '2010-07-08 12:34:56',1,'s' union all
select '2010-07-08 16:17:56',1,'s' union all
select '2010-07-09 11:13:13',2,'s' union all
select '2010-07-09 19:13:13',2,'s' union all
select '2010-07-10 11:13:13',2,'s' union all
select '2010-07-11 11:13:13',1,'s' union all
select '2010-07-12 14:13:13',1,'s'

---查询---
select
开始时间= max(case when a.biaozhi=2 then a.[time] else '' end),
结束时间= max(case when a.biaozhi=1 then a.[time] else '' end),
a.姓名
from
(select *,rn=row_number() over(partition by 姓名 order by [time])
from tb
) a,
(select *,rn=row_number() over(partition by 姓名 order by [time])
from tb
) b
where a.姓名=b.姓名 and a.rn=b.rn+1
and (a.biaozhi=1 and b.biaozhi=2 or a.biaozhi=2 and b.biaozhi=1)
group by a.姓名

---结果---
开始时间 结束时间 姓名
----------------------- ----------------------- ----
2010-07-09 11:13:13.000 2010-07-11 11:13:13.000 s

(1 行受影响)
wjs496249880 2010-07-13
  • 打赏
  • 举报
回复
坐等高手现身
rmljoe 2010-07-13
  • 打赏
  • 举报
回复
create table #tb(Time datetime, biaozhi int ,姓名 varchar(20))
go
insert into #tb
select '2010-07-08 12:34:56','1','s'
union all select '2010-07-08 16:17:56','1','s'
union all select '2010-07-09 11:13:13','2','s'
union all select '2010-07-09 19:13:13','2','s'
union all select '2010-07-10 11:13:13','2','s'
union all select '2010-07-11 11:13:13','1','s'
union all select '2010-07-12 14:13:13','1','s'
go

select top 1
Time as 开始时间,
(select top 1 b.time from #tb b where a.姓名 = b.姓名 and b.biaozhi = 1 and b.Time > a.Time) as 结束时间,
姓名
from #tb a
where biaozhi = 2

/*
开始时间 结束时间 姓名
----------------------- ----------------------- --------------------
2010-07-09 11:13:13.000 2010-07-11 11:13:13.000 s

(1 行受影响)

*/
加载更多回复(6)

34,591

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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