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

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算两条。
...全文
114 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
内容概要:本文提出了一种基于多时段动态电价的电动汽车有序充电策略优化方法,并通过Matlab代码实现对该策略进行仿真验证。该研究聚焦于缓解大规模电动汽车无序充电对电网造成的峰谷负荷压力,通过设计科学的多时段动态电价机制,引导用户在电网负荷低谷时段充电,从而实现削峰填谷、提升电网运行稳定性与可再生能源消纳能力的目标。文中建立了以电网负荷波动最小化和用户充电成本最低化为双重目标的优化模型,结合实际用电数据开展仿真分析,结果表明该策略能显著降低电网峰谷差,提高电力系统的经济性与运行效率。该资源属于“博士论文复现”系列,强调对高水平学术成果的还原与工程化实践,具有较强的理论深度与应用价值。; 适合人群:具备一定电力系统基础知识和Matlab编程能力,从事新能源、智能电网、电动汽车等领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①学习并复现已发表于博士论文或高水平期刊中的电动汽车有序充电优化模型;②研究需求响应机制下电价策略对用户充电行为的引导作用与弹性响应特性;③开发基于动态电价的充电管理系统原型,为智慧能源系统与车网互动(V2G)技术提供算法支持与决策依据; 阅读建议:此资源以Matlab代码为核心,建议读者结合文档内容与代码逐行分析,重点关注目标函数构建、约束条件设定及求解器调用过程,同时尝试修改参数或引入新变量以拓展模型适用范围,加深对优化算法与电力市场机制融合的理解。
内容概要:本文针对传统三电平并网逆变器在谐波抑制、电网不平衡适应性及动态响应方面的不足,提出一种基于有源中点箝位(ANPC)三电平拓扑的高性能并网控制策略。通过融合双极性倍频脉宽调制(DPWMA)、正负序分离锁相技术与电网电压前馈控制,构建“精准同步-扰动补偿-优质调制”的一体化控制体系。ANPC拓扑凭借其开关损耗均衡、中点电位稳定和低输出谐波等优势,为系统提供优良的硬件基础;DPWMA调制在不增加器件开关频率的前提下显著提升等效开关频率,优化并网波形质量;正负序分离锁相技术有效隔离电网负序扰动,保障不平衡工况下的锁相精度与电流对称性;电网电压前馈控制则提前补偿电网扰动,显著增强系统的动态抗扰能力和响应速度。通过Matlab/Simulink搭建仿真模型,在稳态运行、电网不平衡及动态扰动等多种工况下进行验证,结果表明该复合控制策略可大幅降低并网电流谐波含量,提升功率稳定性与系统鲁棒性,适用于复杂电网环境下的大功率高质量并网应用。; 适合人群:具备电力电子、自动控制理论基础,从事新能源发电、并网逆变器研发等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究高电能质量并网逆变器的控制策略设计;②解决弱电网条件下并网系统的稳定性与适应性问题;③掌握DPWMA调制、正负序分离锁相、前馈-反馈复合控制等关键技术的实现方法与协同机制。; 阅读建议:建议结合Matlab/Simulink仿真模型进行实践,重点理解控制策略在不同扰动工况下的响应特性,并可通过修改参数对比传统控制方法,深入掌握复合控制策略的优势与工程应用价值。
内容概要:本文系统研究了在间歇性光伏出力条件下,48V直流母线电压的稳定控制策略及储能系统的双向充放电闭环调控体系。通过Simulink搭建完整的光伏储能直流系统仿真模型,涵盖PV光伏阵列、Boost DC-DC变换器、负载、双向DC-DC变换器与锂离子电池系统,重点实现光伏最大功率点跟踪(MPPT)技术与储能系统协同的削峰填谷控制策略,以解决离网光伏直流微网中存在的功率供需失衡问题。研究深入探讨了电压外环与电流内环的双闭环PI控制结构,确保母线电压稳定,并通过双向DC-DC变换器实现储能电池的智能充放电管理。为进一步提升系统对可再生能源波动性的适应能力,引入多维核密度估计方法生成典型光伏与负荷场景,增强了控制策略的鲁棒性与实用性。; 适合人群:具备电力电子、新能源系统或自动化等相关专业背景,从事微电网、储能控制、光伏系统仿真与能量管理研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于离网或分布式直流微网系统中电压稳定与能量管理策略的设计与验证;②为光伏-储能系统的协同控制、MPPT算法优化、储能充放电调度等提供高保真仿真平台与理论支持;③支撑科研项目申报、高水平论文复现或实际工程项目的系统建模与控制算法开发。; 阅读建议:建议结合Simulink仿真环境动手实践,重点关注MPPT控制模块、双向DC-DC变换器的充放电逻辑及双闭环PI控制器的参数整定过程,同时可拓展引入概率性场景分析或多目标优化算法以进一步提升系统性能与智能化水平。

34,876

社区成员

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

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