Sql 开始日期和结束日期的平均总和
我正在使用SQL Teradata,我有一个这样的表:Sql 开始日期和结束日期的平均总和,sql,teradata,Sql,Teradata,我正在使用SQL Teradata,我有一个这样的表: cust_id start_dt end_dt amount is_current_y_n 12345 1/8/2018 7/8/2018 7044 N 12345 7/9/2018 7/10/2018 8142 N 12345 7/11/2018 7/13/2018 7643 N 12345 7/14/2018 7/14/2018 8630 N
cust_id start_dt end_dt amount is_current_y_n
12345 1/8/2018 7/8/2018 7044 N
12345 7/9/2018 7/10/2018 8142 N
12345 7/11/2018 7/13/2018 7643 N
12345 7/14/2018 7/14/2018 8630 N
12345 7/14/2018 7/19/2018 5597 N
12345 7/20/2018 12/31/9999 5680 Y
我看到的另一个例子是:
cust_id start_dt end_dt amount is_current_y_n
54321 1/1/2015 12/31/9999 8650 Y
我需要使用SQL计算过去的平均金额:
7 days
30 days
90 days
180 days
“平均值”,即如果在过去7天内,金额在第3天从1000变为2000,则平均值应为:
(1000x3+2000x4)/7
我试着用一个日期表连接这个表,但效率不高
有什么有效的方法可以实现这一点吗?可能可以通过递归公共表表达式查询来实现。 展开这些日期范围。
根据每个日期的金额,可以将CTE合并回表中,以获得这些平均值 我无法在TeraData上测试SQL(没有)。
但它几乎可以在RDBMS上工作(可能)
在这种情况下,Teradata的时间特性可能会对您有所帮助。这是由于周期数据类型和要展开的函数造成的 检查此示例以了解此功能和您的意图:
database demo;
create table demoDateExpand (
myID integer
,myUser VARCHAR(100)
,myAmount DECIMAL(10,2)
,startDT DATE
,endDT DATE
) no primary index;
insert into demoDateExpand values (1, 'User01', 2.5, '2018-01-01', '2018-01-05');
insert into demoDateExpand values (2, 'User01', 3.0, '2018-01-08', '2018-01-15');
insert into demoDateExpand values (3, 'User01', 1.5, '2018-01-11', '2018-01-25');
insert into demoDateExpand values (4, 'User02', 2.0, '2018-01-01', '2018-01-15');
insert into demoDateExpand values (5, 'User02', 2.5, '2018-01-05', '2018-01-25');
insert into demoDateExpand values (6, 'User02', 4.5, '2018-01-26', '2018-01-27');
insert into demoDateExpand values (7, 'User03', 1.0, '2018-01-10', '2018-01-15');
insert into demoDateExpand values (8, 'User03', 3.5, '2018-01-16', '2018-01-25');
select myID
,myUser
,myAmount
,startDT
,endDT
,period(startDT, endDT)
from demoDateExpand
;
select myID
,myUser
,myAmount
,BEGIN(myDate)
from demoDateExpand
expand on period(startDT, endDT) AS myDate BY ANCHOR DAY
order by myID, myDate
;
我在一个日期表的帮助下创建了自己的查询:
2017-07-11
2017-07-12
...
我的问题是:
sel
c.cust_id
,avg(case when c.cal_dt between '2017-07-01' and '2018-01-01' then c.amount end) as avg_180
,avg(case when c.cal_dt between '2017-10-01' and '2018-01-01' then c.amount end) as avg_90
,avg(case when c.cal_dt between '2017-12-01' and '2018-01-01' then c.amount end) as avg_30
,avg(case when c.cal_dt between '2017-12-24' and '2018-01-01' then c.amount end) as avg_7
from
(
sel b.cust_id
,a.cal_dt
,b.amount
from
(
sel *
from CALENDAR_DAILY_TABLE
where cal_dt between '2017-07-01' and '2018-01-01'
) as a
join
(
sel *
from MY_TABLE
where (start_dt > '2017-07-01' or end_dt='9999-12-31')
) as b
on b.start_dt<=a.cal_dt and a.cal_dt<=b.end_dt
) as c
where c.cust_id ='12345'
group by c.cust_id
谢谢 在这种情况下,“平均”是什么意思?例如,如果一条记录在过去7天内仅处于活动状态3天,那么它会有什么贡献?此外,您需要显示到目前为止您已经尝试了哪些查询。您是否已经尝试自己解决了这些问题?如果是,请发布您的(非工作)查询。这样,你会学到更多。此外,这个问题不会读作“请做我的工作”。谢谢@我的回答是最好的结果!我不得不在Teradata上玩了很长时间。所以,周期展开法对我来说是新的。但这似乎是一个很酷的功能!该死,他们应该在SQL标准中添加类似的内容。和我一起去。(我有时会怀念其他RDMBS中的这一条款)@DavidKon那么这对TeraData有用吗?我找不到TeraData的任何免费在线测试数据库(如MySql、MsSql、Postgres等)。我也不想在我的笔记本电脑上安装这样一个怪物。所以我只知道它“在理论上”会起作用。
2017-07-11
2017-07-12
...
sel
c.cust_id
,avg(case when c.cal_dt between '2017-07-01' and '2018-01-01' then c.amount end) as avg_180
,avg(case when c.cal_dt between '2017-10-01' and '2018-01-01' then c.amount end) as avg_90
,avg(case when c.cal_dt between '2017-12-01' and '2018-01-01' then c.amount end) as avg_30
,avg(case when c.cal_dt between '2017-12-24' and '2018-01-01' then c.amount end) as avg_7
from
(
sel b.cust_id
,a.cal_dt
,b.amount
from
(
sel *
from CALENDAR_DAILY_TABLE
where cal_dt between '2017-07-01' and '2018-01-01'
) as a
join
(
sel *
from MY_TABLE
where (start_dt > '2017-07-01' or end_dt='9999-12-31')
) as b
on b.start_dt<=a.cal_dt and a.cal_dt<=b.end_dt
) as c
where c.cust_id ='12345'
group by c.cust_id
cust_id avg_180 avg_90 avg_30 avg_7
12345 1.34 1.34 1.34 1.34