提前谢谢。
我有table1
和table2
表1
id || date || name_id || value
001 || 2017-12-01 || 212 || 23.091
002 || 2017-12-02 || 212 || 20.324
003 || 2017-12-03 || 212 || 21.765
004 || 2017-12-04 || 212 || 25.890
005 || 2017-12-01 || 497 || 23.291
006 || 2017-12-02 || 497 || 20.524
007 || 2017-12-03 || 497 || 21.705
008 || 2017-12-04 || 497 || 25.810
009 || 2017-11-01 || 242 || 23.492
010 || 2017-11-02 || 242 || 20.744
011 || 2017-11-03 || 242 || 21.076
012 || 2017-11-04 || 242 || 24.100
013 || 2017-10-01 || 197 || 23.376
014 || 2017-10-02 || 197 || 20.555
015 || 2017-10-03 || 197 || 21.965
016 || 2017-10-04 || 197 || 25.120
表2
id || startup_date || Name
212 || 2017-11-31 || BVUNMI
497 || 2017-11-30 || OUKNGT
242 || 2017-10-26 || TYNBFD
197 || 2017-09-28 || WDRHBG
输出
id || startup_date || Name || Avg_value
212 || 2017-11-31 || BVUNMI || 22.7675
497 || 2017-11-30 || OUKNGT || 23.291
242 || 2017-10-26 || TYNBFD || 22.353
197 || 2017-09-28 || WDRHBG || 22.754
这只是一个示例数据。我有1000条记录,无法弄清楚如何做到这一点。
逻辑是我需要计算从启动日期开始的第一次 30天的平均值。 我必须取每个NAME_ID的平均值并创建,更新到table2中的新列。
我被困住了,不知道怎么走得更远。
感谢任何帮助。
答案 0 :(得分:3)
使用子查询和dateadd
select id,
startup_date,
Name,
(
select avg(value)
from table1 t1
where t1.name_id = t2.id and
date between t2.startup_date and dateadd(day, 30, t2.startup_date)
) Avg_value
from table2 t2
或JOIN
与GROUP BY
select t2.id,
t2.startup_date,
t2.Name,
avg(value) Avg_value
from table2 t2
join table1 t1 on t1.name_id = t2.id and
t1.date between t2.startup_date and dateadd(day, 30, t2.startup_date)
group by t2.id,
t2.startup_date,
t2.Name
答案 1 :(得分:2)
2017-11-31 - 不存在
所以我将其改为2017-11-30,这就是为什么我的结果与你的结果略有不同。遗憾的是,这样的查询可能会对大量数据产生不良影响。如果可能的话,我会建议对数据进行非规范化(将start_date添加到t1中)
SET DATEFORMAT dmy
declare @t1 table(id int , dt date, name_id int, value decimal(15,2))
declare @t2 table(id int , startup_date date, Name nvarchar(100))
insert into @t1(id,dt,name_id,value)
values
(001,convert(date,'2017-12-01',121),212 ,23.091),
(002,convert(date,'2017-12-02',121),212 ,20.324),
(003,convert(date,'2017-12-03',121),212 ,21.765),
(004,convert(date,'2017-12-04',121),212 ,25.890),
(005,convert(date,'2017-12-01',121),497 ,23.291),
(006,convert(date,'2017-12-02',121),497 ,20.524),
(007,convert(date,'2017-12-03',121),497 ,21.705),
(008,convert(date,'2017-12-04',121),497 ,25.810),
(009,convert(date,'2017-11-01',121),242 ,23.492),
(010,convert(date,'2017-11-02',121),242 ,20.744),
(011,convert(date,'2017-11-03',121),242 ,21.076),
(012,convert(date,'2017-11-04',121),242 ,24.100),
(013,convert(date,'2017-10-01',121),197 ,23.376),
(014,convert(date,'2017-10-02',121),197 ,20.555),
(015,convert(date,'2017-10-03',121),197 ,21.965),
(016,convert(date,'2017-10-04',121),197 ,25.120)
insert into @t2(id,startup_date,Name)
VALUES
(212,convert(date,'2017-11-30',121),'BVUNMI'),
(497,convert(date,'2017-11-30',121),'OUKNGT'),
(242,convert(date,'2017-10-26',121),'TYNBFD'),
(197,convert(date,'2017-09-28',121),'WDRHBG')
select
avg(value)
from @t2 t2
inner join @t1 t1 on t2.id = t1.name_id and t1.dt < dateadd(d,30,t2.startup_date)
group by t1.name_id
答案 2 :(得分:1)
试试这个:
select [t2].[Name], AVG([t1].[value]) from [table1] [t1] join [table2] [t2]
on [t1].[name_id] = [t2].[id] and [t1].[date] between [t2].[startup_date] and
DATEADD(day, 30, [t2].[startup_date])
group by [t2].[Name]