疑难问题,求助大神

weixin_40101242 2017-11-30 10:26:32
现在数据表里有这样的数据
日期 小时 分钟
2016-01-01 1 0
2016-01-01 1 5
2016-01-01 1 10

2016-01-01 1 20
2016-01-01 1 25

2016-01-01 1 55
2016-01-01 2 0
2016-01-01 2 5

数据按日期、小时、分钟排序,但中间数据会有缺失的,缺失的数据不管,要获取例如我隔开的那两行数据,
也就是缺失数据夹在中间的数据表中有的数据,并判断其连续的个数,个数为1存入a表,2-3存入b表,大于3存入c表。
如果您看不懂,可参考下图。


...全文
164 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
文盲老顾 2017-11-30
  • 打赏
  • 举报
回复
我15楼的代码里有删除指令,是怕出现你更新时,某个观测记录只有3次及以下,等更新完了,又有新的观测记录入库,这个时候可能需要进的表就变了,所以删除之前位置不对的数据
文盲老顾 2017-11-30
  • 打赏
  • 举报
回复
你的问题应该这么描述 我有一个表,数据如下,[贴图],其中5分钟为一次观测频率,当连续频次为1插入表A,结构如下[贴图],连续频次为2或3,插入表B,结构如下[贴图],其他插入表C 其中如果表A、B、C有相应数据的忽略,不进行插入
文盲老顾 2017-11-30
  • 打赏
  • 举报
回复
;with tt as (
    select *
	,datediff(minute,'2000-1-1',dateadd(minute,分钟,dateadd(hour,小时,convert(datetime,观测日期))))/5 as tn
    from #t  -- 实际使用吧#t换成自己的表名
),ttt as (
	select *,row_number() over(order by tn) as rid from tt a where not exists(select top 1 1 from tt where tn=a.tn-1)
),tttt as (
	select * from ttt a
	cross apply(
		select count(0) as 最大观测次数 from tt where tn>=a.tn and tn<isnull((select tn from ttt where rid=a.rid+1),100000000)
	) b
)
select b.*,(case when 最大观测次数=1 then 'A' when 最大观测次数>3 then 'C' else 'B' end) as tb 
into #t1
from tttt a
cross apply (select * from tt where tn>=a.tn and tn<isnull((select tn from ttt where rid=a.rid+1),100000000)) b

select * from #t1
delete from 表A from #t1 a where a.tb in ('B','C') and a.id=表A.观测id
delete from 表B from #t1 a where a.tb in ('A','C') and a.id=表B.观测id
delete from 表C from #t1 a where a.tb in ('A','B') and a.id=表C.观测id
insert into 表A(观测id,观测日期,小时,分钟) select id,观测日期,小时,分钟 from #t1 a where tb='A' and not exists(select top 1 1 from 表A where 观测id=a.id)
insert into 表B(观测id,观测日期,小时,分钟) select id,观测日期,小时,分钟 from #t1 a where tb='B' and not exists(select top 1 1 from 表B where 观测id=a.id)
insert into 表C(观测id,观测日期,小时,分钟) select id,观测日期,小时,分钟 from #t1 a where tb='C' and not exists(select top 1 1 from 表C where 观测id=a.id)

drop table #t1
weixin_40101242 2017-11-30
  • 打赏
  • 举报
回复
引用 13 楼 ayalicer 的回复:
简单问题 绕那么大圈子~
唉,刚接触么,总喜欢把问题搞复杂。您要不要帮帮我。
  • 打赏
  • 举报
回复
简单问题 绕那么大圈子~
weixin_40101242 2017-11-30
  • 打赏
  • 举报
回复
引用 7 楼 sinat_28984567 的回复:
[quote=引用 6 楼 weixin_40101242 的回复:] [quote=引用 4 楼 sinat_28984567 的回复:] 日期 小时 分钟 2016-01-01 1 0 2016-01-01 1 5 2016-01-01 1 10 2016-01-01 1 20 2016-01-01 1 25 2016-01-01 1 55 2016-01-01 2 0 2016-01-01 2 5 2016-01-01 2 10 2016-01-01 2 15 2016-01-01 2 20 2016-01-01 2 25 如果是这样的,那存入哪些数据? 2016-01-01 2 30
2016-01-01 1 20 2016-01-01 1 25 这两个存入b表, 后边连续的就不管他,这些数据的范围是确定的,首末不去考虑,就在这中间找 [/quote] 这个例子有问题,是下边这个数据 2016-01-01 1 0 2016-01-01 1 5 2016-01-01 1 10 2016-01-01 1 20 2016-01-01 1 25 2016-01-01 1 55 2016-01-01 2 0 2016-01-01 2 5 2016-01-01 2 15 --这两条数据也有问题,和上边的怎么存入? 2016-01-01 2 20 2016-01-01 2 30 2016-01-01 2 35 [/quote] 我把事情讲复杂了,是这样的。 这是我表中的数据,现在要取出这些数据中间的缺失数据,然后判断它们的个数,个数为1存入a表,2-3存入b表,大于3存入c表。
weixin_40101242 2017-11-30
  • 打赏
  • 举报
回复
引用 9 楼 superwfei 的回复:
;with tt as (
select *
,datediff(minute,'2000-1-1',dateadd(minute,分钟,dateadd(hour,小时,convert(datetime,观测日期))))/5 as tn
from #t -- 实际使用吧#t换成自己的表名
),ttt as (
select *,row_number() over(order by tn) as rid from tt a where not exists(select top 1 1 from tt where tn=a.tn-1)
),tttt as (
select * from ttt a
cross apply(
select count(0) as 最大观测次数 from tt where tn>=a.tn and tn<isnull((select tn from ttt where rid=a.rid+1),100000000)
) b
)
select b.*,(case when 最大观测次数=1 then 'A' when 最大观测次数>3 then 'C' else 'B' end)
-- 可以在这个位置增加一个 into #t1 ,将结果放到临时表中,然后按照临时表的内容依次插入数据到不同的表中
from tttt a
cross apply (select * from tt where tn>=a.tn and tn<isnull((select tn from ttt where rid=a.rid+1),100000000)) b

我把事情讲复杂了,是这样的。

这是我表中的数据,现在要取出这些数据中间的缺失数据,然后判断它们的个数,个数为1存入a表,2-3存入b表,大于3存入c表。
weixin_40101242 2017-11-30
  • 打赏
  • 举报
回复
引用 9 楼 superwfei 的回复:
;with tt as (
select *
,datediff(minute,'2000-1-1',dateadd(minute,分钟,dateadd(hour,小时,convert(datetime,观测日期))))/5 as tn
from #t -- 实际使用吧#t换成自己的表名
),ttt as (
select *,row_number() over(order by tn) as rid from tt a where not exists(select top 1 1 from tt where tn=a.tn-1)
),tttt as (
select * from ttt a
cross apply(
select count(0) as 最大观测次数 from tt where tn>=a.tn and tn<isnull((select tn from ttt where rid=a.rid+1),100000000)
) b
)
select b.*,(case when 最大观测次数=1 then 'A' when 最大观测次数>3 then 'C' else 'B' end)
-- 可以在这个位置增加一个 into #t1 ,将结果放到临时表中,然后按照临时表的内容依次插入数据到不同的表中
from tttt a
cross apply (select * from tt where tn>=a.tn and tn<isnull((select tn from ttt where rid=a.rid+1),100000000)) b

您看啊,
这些从一开始就连续的是不考虑的,但是也存入了c表
文盲老顾 2017-11-30
  • 打赏
  • 举报
回复
;with tt as (
    select *
	,datediff(minute,'2000-1-1',dateadd(minute,分钟,dateadd(hour,小时,convert(datetime,观测日期))))/5 as tn
    from #t  -- 实际使用吧#t换成自己的表名
),ttt as (
	select *,row_number() over(order by tn) as rid from tt a where not exists(select top 1 1 from tt where tn=a.tn-1)
),tttt as (
	select * from ttt a
	cross apply(
		select count(0) as 最大观测次数 from tt where tn>=a.tn and tn<isnull((select tn from ttt where rid=a.rid+1),100000000)
	) b
)
select b.*,(case when 最大观测次数=1 then 'A' when 最大观测次数>3 then 'C' else 'B' end) 
-- 可以在这个位置增加一个 into #t1 ,将结果放到临时表中,然后按照临时表的内容依次插入数据到不同的表中
from tttt a
cross apply (select * from tt where tn>=a.tn and tn<isnull((select tn from ttt where rid=a.rid+1),100000000)) b
weixin_40101242 2017-11-30
  • 打赏
  • 举报
回复
引用 7 楼 sinat_28984567 的回复:
[quote=引用 6 楼 weixin_40101242 的回复:] [quote=引用 4 楼 sinat_28984567 的回复:] 日期 小时 分钟 2016-01-01 1 0 2016-01-01 1 5 2016-01-01 1 10 2016-01-01 1 20 2016-01-01 1 25 2016-01-01 1 55 2016-01-01 2 0 2016-01-01 2 5 2016-01-01 2 10 2016-01-01 2 15 2016-01-01 2 20 2016-01-01 2 25 如果是这样的,那存入哪些数据? 2016-01-01 2 30
2016-01-01 1 20 2016-01-01 1 25 这两个存入b表, 后边连续的就不管他,这些数据的范围是确定的,首末不去考虑,就在这中间找 [/quote] 这个例子有问题,是下边这个数据 2016-01-01 1 0 2016-01-01 1 5 2016-01-01 1 10 2016-01-01 1 20 2016-01-01 1 25 2016-01-01 1 55 2016-01-01 2 0 2016-01-01 2 5 2016-01-01 2 15 --这两条数据也有问题,和上边的怎么存入? 2016-01-01 2 20 2016-01-01 2 30 2016-01-01 2 35 [/quote] 2016-01-01 1 20 2016-01-01 1 25 存入b表; 2016-01-01 1 55 2016-01-01 2 0 2016-01-01 2 5 存入c表; 2016-01-01 2 15 2016-01-01 2 20 存入b表
二月十六 2017-11-30
  • 打赏
  • 举报
回复
引用 6 楼 weixin_40101242 的回复:
[quote=引用 4 楼 sinat_28984567 的回复:] 日期 小时 分钟 2016-01-01 1 0 2016-01-01 1 5 2016-01-01 1 10 2016-01-01 1 20 2016-01-01 1 25 2016-01-01 1 55 2016-01-01 2 0 2016-01-01 2 5 2016-01-01 2 10 2016-01-01 2 15 2016-01-01 2 20 2016-01-01 2 25 如果是这样的,那存入哪些数据? 2016-01-01 2 30
2016-01-01 1 20 2016-01-01 1 25 这两个存入b表, 后边连续的就不管他,这些数据的范围是确定的,首末不去考虑,就在这中间找 [/quote] 这个例子有问题,是下边这个数据 2016-01-01 1 0 2016-01-01 1 5 2016-01-01 1 10 2016-01-01 1 20 2016-01-01 1 25 2016-01-01 1 55 2016-01-01 2 0 2016-01-01 2 5 2016-01-01 2 15 --这两条数据也有问题,和上边的怎么存入? 2016-01-01 2 20 2016-01-01 2 30 2016-01-01 2 35
weixin_40101242 2017-11-30
  • 打赏
  • 举报
回复
引用 4 楼 sinat_28984567 的回复:
日期 小时 分钟 2016-01-01 1 0 2016-01-01 1 5 2016-01-01 1 10 2016-01-01 1 20 2016-01-01 1 25 2016-01-01 1 55 2016-01-01 2 0 2016-01-01 2 5 2016-01-01 2 10 2016-01-01 2 15 2016-01-01 2 20 2016-01-01 2 25 如果是这样的,那存入哪些数据? 2016-01-01 2 30
2016-01-01 1 20 2016-01-01 1 25 这两个存入b表, 后边连续的就不管他,这些数据的范围是确定的,首末不去考虑,就在这中间找
二月十六 2017-11-30
  • 打赏
  • 举报
回复
上边的例子有点问题,如果是这种情况怎么办? 2016-01-01 1 0 2016-01-01 1 5 2016-01-01 1 10 2016-01-01 1 20 2016-01-01 1 25 2016-01-01 1 55 2016-01-01 2 0 2016-01-01 2 5 2016-01-01 2 15 2016-01-01 2 20 2016-01-01 2 30 2016-01-01 2 35 如果是这样的,那存入哪些数据?
二月十六 2017-11-30
  • 打赏
  • 举报
回复
日期 小时 分钟 2016-01-01 1 0 2016-01-01 1 5 2016-01-01 1 10 2016-01-01 1 20 2016-01-01 1 25 2016-01-01 1 55 2016-01-01 2 0 2016-01-01 2 5 2016-01-01 2 10 2016-01-01 2 15 2016-01-01 2 20 2016-01-01 2 25 如果是这样的,那存入哪些数据? 2016-01-01 2 30
weixin_40101242 2017-11-30
  • 打赏
  • 举报
回复
引用 2 楼 baidu_36457652 的回复:
试下把 表的3个字段合到一起,然后做一个left join 连接的条件是 时间+5分钟, 然后 做一个标识,没有连接的为0 连接的为1 然后统计 两个标识0之间的1的个数 再插入到对应表。
sql我只会简单的增删查改,您说的这些不是很懂。
  • 打赏
  • 举报
回复
试下把 表的3个字段合到一起,然后做一个left join 连接的条件是 时间+5分钟, 然后 做一个标识,没有连接的为0 连接的为1 然后统计 两个标识0之间的1的个数 再插入到对应表。
weixin_40101242 2017-11-30
  • 打赏
  • 举报
回复
求助
weixin_40101242 2017-11-30
  • 打赏
  • 举报
回复
引用 16 楼 superwfei 的回复:
你的问题应该这么描述 我有一个表,数据如下,[贴图],其中5分钟为一次观测频率,当连续频次为1插入表A,结构如下[贴图],连续频次为2或3,插入表B,结构如下[贴图],其他插入表C 其中如果表A、B、C有相应数据的忽略,不进行插入
谢谢您一直的帮助。我是想查找一个时间段内无记录的数据,判断无记录数据的观测频率,然后再插入其它表。 您上边的代码,操作时间还是太长了。

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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