查询语句如下:
select *,rn=row_number() over(partition by 地区, 频道, 名称 order by 处理后日期) into #1 from [剧目表]
create index index_临时表1 on #1(地区,频道,名称,处理后日期)
--按10天间隔设置标志
select *,flag=case when (select isnull(datediff(d,处理后日期,a.处理后日期),10) from #1 where 地区=a.地区 and 频道=a.频道 and 名称=a.名称 and rn=a.rn-1)<10 then 0 else 1 end
into #2
from #1 a
create index index_临时表2 on #2(地区,频道,名称,处理后日期)
--合并次数并更新到源表
update [剧目表]
set [播出次数] = b.次数
from
(select 地区,频道,名称,处理后日期,次数=(select sum(flag) from #2 where 地区=a.地区 and 频道=a.频道 and 名称=a.名称 and rn<=a.rn)
from #2 a) b
where [剧目表].地区=b.地区 and [剧目表].频道=b.频道 and [剧目表].名称=b.名称 and [剧目表].处理后日期=b.处理后日期首先想把他转成SQL的存储过程,然后再在access VBA中执行这个存储过程,刚刚开始学习SQL,请多多帮助,谢谢
select *,rn=row_number() over(partition by 地区, 频道, 名称 order by 处理后日期) into #1 from [剧目表]
create index index_临时表1 on #1(地区,频道,名称,处理后日期)
--按10天间隔设置标志
select *,flag=case when (select isnull(datediff(d,处理后日期,a.处理后日期),10) from #1 where 地区=a.地区 and 频道=a.频道 and 名称=a.名称 and rn=a.rn-1)<10 then 0 else 1 end
into #2
from #1 a
create index index_临时表2 on #2(地区,频道,名称,处理后日期)
--合并次数并更新到源表
update [剧目表]
set [播出次数] = b.次数
from
(select 地区,频道,名称,处理后日期,次数=(select sum(flag) from #2 where 地区=a.地区 and 频道=a.频道 and 名称=a.名称 and rn<=a.rn)
from #2 a) b
where [剧目表].地区=b.地区 and [剧目表].频道=b.频道 and [剧目表].名称=b.名称 and [剧目表].处理后日期=b.处理后日期首先想把他转成SQL的存储过程,然后再在access VBA中执行这个存储过程,刚刚开始学习SQL,请多多帮助,谢谢
as
begin
select *,rn=row_number() over(partition by 地区, 频道, 名称 order by 处理后日期) into #1 from [剧目表]
select *,flag=case when (select isnull(datediff(d,处理后日期,a.处理后日期),10) from #1 where 地区=a.地区 and 频道=a.频道 and 名称=a.名称 and rn=a.rn-1) <10 then 0 else 1 end
into #2
from #1 a
update [剧目表]
set [播出次数] = b.次数
from
(select 地区,频道,名称,处理后日期,次数=(select sum(flag) from #2 where 地区=a.地区 and 频道=a.频道 and 名称=a.名称 and rn <=a.rn)
from #2 a) b
where [剧目表].地区=b.地区 and [剧目表].频道=b.频道 and [剧目表].名称=b.名称 and [剧目表].处理后日期=b.处理后日期
drop table #1
drop table #2
end
这样?