Sql 笛卡尔积(与分组依据求和)
Sql cartesian product (summing with group by)
我正在尝试计算 table important_stock_dates
中一组股票在过去三十天的成交量总和。 table all_stock_dates
包含相同的股票,但具有所有日期的交易量,而不仅仅是特定日期。
示例数据
all_stock_dates
stockid, date, volume
0231245, 20060314, 153
0231245, 20060315, 154
2135411, 20060314, 23
important_stock_dates
stockid, date, thirtydaysprior
0231245, 20060314, 20060130
0231245, 20060315, 20060201
2135411, 20060314, 20060130
我的代码
create table sum_trading_volume as
select a.stockid, a.date, sum(b.volume) as thirty_day_volume
from important_stock_dates a, all_stock_dates b
where b.date<a.date AND b.date ge a.thirtydaysprior
group by a.stockid, a.date;
期望的结果
A table 包含来自 important_stock_dates
的所有观察值,它还具有基于 all_stock_dates
.[=20 中匹配的 stockid 和日期的前 30 天的交易量总和=]
问题
我 运行 遇到的问题是 important_stock_dates
有 1500 万个观测值,而 all_stock_dates
有 3.5 亿个。它用掉了几百 GB 的交换文件 运行 此代码(最大化硬盘驱动器)然后中止。我看不到如何优化代码。我在 Whosebug 或 Google.
上找不到类似的问题
据推测,您想要的查询加入 stockid
:
create table sum_trading_volume as
select isd.stockid, isd.date, sum(asd.volume) as thirty_day_volume
from important_stock_dates isd join
all_stock_dates asd
on isd.stockid = asd.stockid and
asd.date < isd.date and asd.date >= isd.thirtydaysprior
group by isd.stockid, isd.date;
如果这有效,它可能会 运行 完成。
我正在尝试计算 table important_stock_dates
中一组股票在过去三十天的成交量总和。 table all_stock_dates
包含相同的股票,但具有所有日期的交易量,而不仅仅是特定日期。
示例数据
all_stock_dates
stockid, date, volume
0231245, 20060314, 153
0231245, 20060315, 154
2135411, 20060314, 23
important_stock_dates
stockid, date, thirtydaysprior
0231245, 20060314, 20060130
0231245, 20060315, 20060201
2135411, 20060314, 20060130
我的代码
create table sum_trading_volume as
select a.stockid, a.date, sum(b.volume) as thirty_day_volume
from important_stock_dates a, all_stock_dates b
where b.date<a.date AND b.date ge a.thirtydaysprior
group by a.stockid, a.date;
期望的结果
A table 包含来自 important_stock_dates
的所有观察值,它还具有基于 all_stock_dates
.[=20 中匹配的 stockid 和日期的前 30 天的交易量总和=]
问题
我 运行 遇到的问题是 important_stock_dates
有 1500 万个观测值,而 all_stock_dates
有 3.5 亿个。它用掉了几百 GB 的交换文件 运行 此代码(最大化硬盘驱动器)然后中止。我看不到如何优化代码。我在 Whosebug 或 Google.
据推测,您想要的查询加入 stockid
:
create table sum_trading_volume as
select isd.stockid, isd.date, sum(asd.volume) as thirty_day_volume
from important_stock_dates isd join
all_stock_dates asd
on isd.stockid = asd.stockid and
asd.date < isd.date and asd.date >= isd.thirtydaysprior
group by isd.stockid, isd.date;
如果这有效,它可能会 运行 完成。