◆◆◆◆◆关于时间的统计问题!

surfl 2003-12-08 12:11:32

T1(tid,tuserno,tname,tTime,nType)

1 00002 小章 2001-10-10 12:12:00 A
2 00002 小章 2001-10-10 12:13:00 A
3 00002 小章 2001-10-10 12:14:00 A
4 00008 小王 2001-10-10 12:12:00 B

现在需要查询 在某一段时间内某一类nType的记录总和。
例:
SELECT COUNT(*) FROM t1 WHERE
tTime BETWEEN '2001-01-01 00:00:01' AND '2001-09-01 24:59:59'
AND nType='A'

但现在的问题是,如果某一个人在某一天的某一个类型中两次时间间隔<=1只算一次
例:
1 00002 小章 2001-10-10 12:12:00 A
2 00002 小章 2001-10-10 12:13:00 A(只算一条记录)

如果是
10 00002 小章 2001-10-10 12:12:00 A
11 00002 小章 2001-10-10 12:13:00 A
12 00002 小章 2001-10-10 12:14:00 A
13 00002 小章 2001-10-10 12:15:00 A(算两条)
即如果两条记录时间间隔小于1,取时间小的一条记录。所以
10,11中取10=》10,12,13=》10和12间隔大于1,所以10,12,13=>10,12算两条。
...全文
64 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
azyue 2003-12-22
  • 打赏
  • 举报
回复
数据库的sql语言做还不如使用高级编程语言直接读去记录比较呢,可能速度要受影响,
刚开始还不太明白意思。看来功底太差,。
感慨修行中…………………………
victorycyz 2003-12-09
  • 打赏
  • 举报
回复
测试数据(省略了人名):

1 00002 2001-10-10 12:12:00 A
2 00002 2001-10-10 12:13:00 A
3 00002 2001-10-10 12:14:00 A
4 00008 2001-10-10 12:12:00 A
5 00008 2001-10-10 12:13:00 b
6 00002 2001-10-10 12:15:00 A
7 00002 2001-10-10 12:15:00 b

运行结果:
A 3
b 2
victorycyz 2003-12-09
  • 打赏
  • 举报
回复
我上面的改一下:
declare @n0 varchar(5),@tm0 datetime,@tp0 char
declare @i int,@n varchar(5),@tm datetime,@tp char

create table #t (tid int)
insert into #t select top 1 tid from t1 order by tuserno,ntype,ttime

declare curA cursor
for
select tid,tuserno,ntype,ttime from t1 order by tuserno,ntype,ttime

open curA

fetch next from curA into @i,@n0,@tp0,@tm0
fetch next from curA into @i,@n,@tp,@tm
while @@fetch_status=0
begin
if not (@n=@n0 and @tp=@tp0 and datediff(n,@tm0,@tm)<=1)
begin
insert into #t values (@i)
set @n0=@n
set @tp0=@tp
set @tm0=@tm
end
fetch next from curA into @i,@n,@tp,@tm
end

close curA
deallocate curA

select ntype,count(ntype) from t1 a join #t b on a.tid=b.tid group by a.ntype

drop table #t
zjcxc 元老 2003-12-09
  • 打赏
  • 举报
回复
--下面是数据测试

--测试数据
declare @T1 table(tid int,tname varchar(10),tTime datetime)
insert into @t1
select 1,'小张','2001-10-10 12:12:00' --这里算一次
union all select 2,'小张','2001-10-10 12:13:00' --这里不算因为<=60秒
union all select 3,'小张','2001-10-10 12:14:00' --这里也应该算一次,因为在前两
--次中取的是小的一个即1号记录
union all select 4,'小张','2001-10-10 12:15:00' --过滤
union all select 5,'小张','2001-10-10 12:16:00' --算一次
union all select 6,'小张','2001-10-10 12:17:00' --过滤
union all select 7,'小王','2001-10-10 12:12:00'
union all select 8,'小李','2001-10-10 12:13:00'
union all select 9,'小刘','2001-10-10 12:14:01'

--数据统计--分组全部统计
select tname,次数=count(*)
from @t1 a
where isnull((select sum(1) from @t1 a1 where tname=a.tname and tid<a.tid
and exists(select 1 from @t1 where tname=a.tname and tid<a.tid
and datediff(ss,ttime,a.ttime)<=60)),0) %2=0
group by tname

/*--测试结果

tname 次数
---------- -----------
小李 1
小刘 1
小王 1
小张 3

(所影响的行数为 4 行)
--*/

surfl 2003-12-08
  • 打赏
  • 举报
回复
那里有问题呢??
playyuer 2003-12-08
  • 打赏
  • 举报
回复
问题提得有毛病!
victorycyz 2003-12-08
  • 打赏
  • 举报
回复
测试数据(省略了人名):

1 00002 2001-10-10 12:12:00 A
2 00002 2001-10-10 12:13:00 A
3 00002 2001-10-10 12:14:00 A
4 00008 2001-10-10 12:12:00 A
5 00008 2001-10-10 12:13:00 b
6 00002 2001-10-10 12:15:00 A
7 00002 2001-10-10 12:15:00 b

运行结果:
1 00002 2001-10-10 12:12:00 A
3 00002 2001-10-10 12:14:00 A
4 00008 2001-10-10 12:12:00 A
5 00008 2001-10-10 12:13:00 b
7 00002 2001-10-10 12:15:00 b
victorycyz 2003-12-08
  • 打赏
  • 举报
回复

declare @n0 varchar(5),@tm0 datetime,@tp0 char
declare @i int,@n varchar(5),@tm datetime,@tp char

create table #t (tid int)
insert into #t select top 1 tid from t1 order by tuserno,ntype,ttime

declare curA cursor
for
select tid,tuserno,ntype,ttime from t1 order by tuserno,ntype,ttime

open curA

fetch next from curA into @i,@n0,@tp0,@tm0
fetch next from curA into @i,@n,@tp,@tm
while @@fetch_status=0
begin
if @n=@n0 and @tp=@tp0 and datediff(n,@tm0,@tm)<=1
begin
set @i=@i
end
else
begin
insert into #t values (@i)
set @n0=@n
set @tp0=@tp
set @tm0=@tm
end
fetch next from curA into @i,@n,@tp,@tm
end

close curA
deallocate curA

select a.* from t1 a join #t b on a.tid=b.tid

drop table #t
surfl 2003-12-08
  • 打赏
  • 举报
回复
其实就是,对于同一个人现按照姓名和时间排序。
然后,对于同一人过滤掉前后两次时间间隔<=60秒的记录。
最后,用Count(*)分组统计!
就是在第二步不知道怎么做
surfl 2003-12-08
  • 打赏
  • 举报
回复
不过,如果是以下的情况呢??
select 1,'小张','2001-10-10 12:12:00' --这里算一次
union all select 2,'小张','2001-10-10 12:13:00' --这里不算因为<=60秒
union all select 3,'小张','2001-10-10 12:14:00' --这里也应该算一次,因为在前两
--次中取的是小的一个即1号记录
union all select 4,'小张','2001-10-10 12:15:00' --过滤
union all select 5,'小张','2001-10-10 12:16:00' --算一次
union all select 6,'小张','2001-10-10 12:17:00' -过滤
surfl 2003-12-08
  • 打赏
  • 举报
回复
谢谢 zjcxc(邹建) 兄!刚刚没看到!
zjcxc 元老 2003-12-08
  • 打赏
  • 举报
回复
--下面是数据测试

--测试数据
declare @T1 table(tid int,tname varchar(10),tTime datetime)
insert into @t1
select 1,'小张','2001-10-10 12:12:00' --这里与下面是一次
union all select 2,'小张','2001-10-10 12:13:00'
union all select 3,'小张','2001-10-10 12:14:01' --这里与前面相差超过了1分钟
union all select 4,'小王','2001-10-10 12:12:00'
union all select 5,'小李','2001-10-10 12:13:00'
union all select 6,'小刘','2001-10-10 12:14:01'

--数据统计--分组全部统计
select tname,消费次数=count(*) from(
select * from @t1 a
where not exists(select 1 from @t1 where tname=a.tname
and tid<a.tid and datediff(ss,ttime,a.ttime)<=60)
) a
where ttime between '2001-01-01 00:00:00' and '2002-11-30 23:59:59'
group by tname

/*--测试结果
tname
---------- -----------
小李 1
小刘 1
小王 1
小张 2

(所影响的行数为 4 行)
--*/
zjcxc 元老 2003-12-08
  • 打赏
  • 举报
回复
--楼主有没有看我的回复,我上面不是给出了吗?

select tname,消费次数=count(*) from(
select * from t1 a
where not exists(select 1 from t1 where tname=a.tname
and tid<a.tid and datediff(ss,ttime,a.ttime)<=60)
) a
where ttime between '2001-01-01 00:00:00' and '2002-11-30 23:59:59'
group by tname
surfl 2003-12-08
  • 打赏
  • 举报
回复
看来是我没说清楚,我简化一下题目

T1 (tID,tName,tTime) //ID号,用户姓名,消费时间
1 小张 2001-10-10 12:12:00
2 小张 2001-10-10 12:13:00
3 小张 2001-10-10 12:14:01
4 小王 2001-10-10 12:12:00
5 小李 2001-10-10 12:13:00
6 小刘 2001-10-10 12:14:01

现在需要统计一段时间内所有人的消费次数总和,
但如果是某一个人的前后两次消费时间间隔《=1分中,
则只算一次消费。

zjcxc 元老 2003-12-08
  • 打赏
  • 举报
回复
--下面是数据测试

--测试数据
declare @T1 table(tid int,tuserno varchar(6),tname varchar(10),tTime datetime,nType varchar(4))
insert into @t1
select 1,'00002','小章','2001-10-10 12:12:00','A'
union all select 2,'00002','小章','2001-10-10 12:13:00','A'
union all select 3,'00002','小章','2001-10-10 12:14:00','A'
union all select 3,'00002','小章','2001-10-10 12:17:00','A'
union all select 4,'00008','小王','2001-10-10 12:12:00','B'

--数据统计--指定条件统计
select count(*) from(
select * from @t1 a
where not exists(select 1 from @t1 where tuserno=a.tuserno and tname=a.tname
and tid<a.tid and datediff(ss,ttime,a.ttime)<=60)
) a
where tTime BETWEEN '2001-01-01 00:00:01' AND '2001-12-01 23:59:59'
and ntype='A'

--数据统计--分组全部统计
select ntype,count(*) from(
select * from @t1 a
where not exists(select 1 from @t1 where tuserno=a.tuserno and tname=a.tname
and tid<a.tid and datediff(ss,ttime,a.ttime)<=60)
) a group by ntype

/*--测试结果
--数据统计--指定条件统计

-----------
2

(所影响的行数为 1 行)

--数据统计--分组全部统计
ntype
----- -----------
A 2
B 1

(所影响的行数为 2 行)
--*/
zjcxc 元老 2003-12-08
  • 打赏
  • 举报
回复
--这样统计就行了

select count(*) from(
select * from t1 a
where not exists(select 1 from t1 where tuserno=a.tuserno and tname=a.tname
and tid<a.tid and datediff(ss,ttime,a.ttime)<=60)
) a
where tTime BETWEEN '2001-01-01 00:00:01' AND '2001-12-01 23:59:59'
and ntype='A'
azyue 2003-12-08
  • 打赏
  • 举报
回复
有毛病
azyue 2003-12-08
  • 打赏
  • 举报
回复
不懂,为什么只有一条记录呀。??
应该两条的呀。为什么呢????????????????????????????
szyhy810518 2003-12-08
  • 打赏
  • 举报
回复
up

34,590

社区成员

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

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