这是我的FoxPro程序。 Select子句导致错误。
PROCEDURE IFRS_Split_Cashflows_2_v7_4_CSV
PARAMETERS model_dir, cube2_output_path, output_path, scenario, version
temp_dir = GETENV("TEMP")
**** Load up utility function and Create an output folder ****
SET PROCEDURE TO model_dir + "\CUBE2_" + version + "\PRGS\Utilities_" + version + ".prg"
lcTextFile = cube2_output_path + "\IFRS_Split_Cashflows_2\run_" + scenario + ".csv"
Do delete_file WITH lcTextFile
Do create_folder WITH cube2_output_path + "\IFRS_Split_Cashflows_2"
**** Assign variables to the input files ****
moses_output_file = output_path + "\annuities~main10.dbf"
SELECT 0
Use (moses_output_file) ALIAS cube_output1
moses_output_file = output_path + "\annuities~main10~reinsured.dbf"
SELECT 0
Use (moses_output_file) ALIAS cube_output2
moses_output_file = output_path + "\annuities~main10~reinsurance_premium.dbf"
SELECT 0
Use (moses_output_file) ALIAS cube_output3
**** Split the group string and select relevant data ****
SELECT ;
c1.t_from AS t_from, ;
c1.cal_mth as calendar_month, ;
c1.cal_yr as calendar_year, ;
substr(c1.group,1,3) AS BUS_LINE, ;
padr( IIF( isnumeric( strextract(c1.group, "|", "|", 1, 1), ""), ;
substr(c1.group,1,4) + strextract(c1.group, "|", "|", 1, 1), ;
strextract( c1.group,"|", "|",1, 1)), 10) AS reinsurance_book,;
padr( strextract(c1.group, "|","|", 3,1),10) AS Esc_Index,;
padr( IIF( left(c1.group,3) = "LRT", ;
strextract(c1.group, "|", "|",5,1), ;
strextract(c1.group,"|","|",4,1)) ,10) AS MAP_ELIG,;
padr( IIF( left(c1.group,3) = "LRT", ;
strextract( trim(c1.group - "|"),"|","|", 6, 1), ;
strextract( trim(c1.group - "|"),"|","|", 5,1)), 10) AS MAP_STAT,;
SUM(c1.ann_ben_if) AS ann_ben_if, ;
SUM(c2.dth_out) AS c2death_outgo, ;
SUM(c3.dth_out) AS c3death_outgo;
FROM ;
cube_output1 c1, ;
cube_output2 c2, ;
cube_output3 c3;
INTO ;
CURSOR temp READWRITE;
WHERE ;
c1.t_from = 0 ;
AND ( ( c1.t_from = c2.t_from AND c1.group = c2.group) ;
OR ( c1.t_from = c3.t_from AND c1.group = c3.group)) ;
group by ;
c1.t_from, ;
c1.cal_mth, ;
c1.cal_yr, ;
BUS_LINE, ;
reinsurance_book, ;
Esc_Index, ;
MAP_ELIG, ;
MAP_STAT
**** Copy to a CSV file ****
COPY TO cube2_output_path + "\IFRS_Split_Cashflows_2\run_" + scenario + ".csv" TYPE CSV
它会生成错误,"文件c:\ users \ jc \ appdata \ local \ temp \ 00002jo9000h.tmp太大。其他任务失败。"
我实际上从select语句中删除了几个与c2和c3相关的字段。我还将where子句的第一部分从c1.t_from > 0
更改为c1.t_from = 0
,以减少观察。
错误仍然存在。据我所知,临时文件的上限文件大小为2GB。大小方面,输入文件如下:c1为275MB,C2为275MB,c3为191MB。鉴于此,我对临时文件太大感到惊讶。为什么它太大了?是否有任何方法可以改进上面的Select语句,以正确返回请求的字段?
答案 0 :(得分:2)
我认为您在查询中获得笛卡尔结果,这会导致您死亡。
您的查询未使用C2和C3别名引用中的任何实际列值,只是确保在C2或C3表的EITHER中找到给定的T_From和Group。
为了消除您的临时文件崩溃,我会确保您在每个C2和C3表上都有一个基于T_From和Group的索引,例如
moses_output_file = output_path + "\annuities~main10~reinsured.dbf"
Use (moses_output_file) ALIAS cube_output2 EXCLUSIVE
index on str( T_From ) + Group tag TFromGroup
moses_output_file = output_path + "\annuities~main10~reinsurance_premium.dbf"
SELECT 0
Use (moses_output_file) ALIAS cube_output3 EXCLUSIVE
index on str( T_From ) + Group tag TFromGroup
索引可以在此功能外部创建,并始终保持可用状态。如果索引尚不存在,则可以在此使用索引。
现在,查询修复了。将FROM / WHERE子句更改为以下内容。我正在做的是从C1表到子查询结果的SQL-JOIN。由于您只关心提取其他表中的记录的记录,我正在进行PreCheck查询以获取找到的不同T_From / Group记录。然后只用T_From = 0拉动那些C1记录。
请注意,PreCheck查询也在T_From = 0上应用了where子句,所以我不能加入那些在T_From = 0时找到它们的组。你将完全消除任何可能的笛卡尔结果,因为内部查询只会生成DISTINCT“组”记录。
选择(所有其他字段相同...最后一个字段添加逗号)
C2Sum.C2Death_Outgo,;
C3Sum.C3Death_Outgo;
FROM ;
cube_output1 c1;
LEFT JOIN ( select c2.Group,;
SUM(c2.dth_out) AS c2death_outgo ;
FROM cube_output2 c2;
WHERE c2.t_from = 0;
GROUP BY c2.Group ) C2Sum ;
on c1.t_group = C2Sum.Group; ;
LEFT JOIN ( select c3.Group, ;
SUM(c3.dth_out) AS c3death_outgo;
FROM cube_output3 c3 ;
WHERE c3.t_from = 0 ;
GROUP BY c3.Group ) C3Sum ;
ON c1.t_group = C3Sum.group;
WHERE ;
c1.t_from = 0 ;
AND ( NOT ISNULL( c2Sum.Group ) OR NOT ISNULL( c3Sum.Group ))
我在这里做的是每个表的分组,以防止它们之间的笛卡尔。我正在对他们两个进行LEFT-JOIN,因为不知道你的数据,可能是每个C2 / C3表中的记录,并且不想扭曲总结。
通过完成where子句,我正在寻找EFTER LEFT JOIN到NOT BE NULL,这意味着给定组有一条记录。再次,EITHER方面或BOTH可以有一个记录,因此它将被计为ONCE,不会导致重复。
现在,由于group by的金额仅在组级别,并且您的OUTER查询按照公交线路,保险簿,索引等的更多部分进行分组,无论它们在哪个子分类中,GROUP金额对所有人来说都是一样的,这可能不是你想要的。
最后,你的重度解析,填充,字符串提取似乎不是一种很好的分类方式,但对于单个列而言可能更有效......只是一个想法。
答案 1 :(得分:0)
正如德拉普所说,你得到了一个笛卡儿(并非真正但接近它)的结果,因此你的临时文件变得越来越大。如果你做一个小模型:
<强> cube1 强>
t_from cal_mth cal_yr ann_be_if group
------ ------- ------ --------- -----
0 1 2016 100 1
0 2 2016 800 2
1 3 2016 500 3
<强> CUBE2 强>
t_from fieldX group
------ ------ -----
0 1 1
0 2 1
0 3 1
<强> cube3 强>
t_from fieldY group
------ ------ -----
0 1 2
0 2 2
0 3 2
0 4 2
使用您的SQL,这将导致从cube1中选择2行3 * 4次(您的条件匹配cube2中的3行和cube3中的4行),这将是24行。由于你重复选择相同的行3 * 4次,你也会重复ann_be_if的值3 * 4次,并将它们加起来(800 + 100)* 12 = 10800.这不太可能你想要的。
当您有联接时(隐式或隐式),您不应该进行聚合 显式)与其他表(除非你确定你不会 这种重复 - 即加入样本客户,订单或orditems 表格不会出现这种重复)。
再次读取您的SQL,第二个和第三个表在结果上没有任何角色,只是您只对它们进行“存在”检查。因此,您可以将代码重写为:
**** Assign variables to the input files ****
*!* moses_output_file = output_path + "\annuities~main10.dbf"
*!* SELECT 0
*!* Use (moses_output_file) ALIAS cube_output1
*!* moses_output_file = output_path + "\annuities~main10~reinsured.dbf"
*!* SELECT 0
*!* Use (moses_output_file) ALIAS cube_output2
*!* moses_output_file = output_path + "\annuities~main10~reinsurance_premium.dbf"
*!* SELECT 0
*!* Use (moses_output_file) ALIAS cube_output3
cube_output1 = m.output_path + "\annuities~main10.dbf"
cube_output2 = m.output_path + "\annuities~main10~reinsured.dbf"
cube_output3 = m.output_path + "\annuities~main10~reinsurance_premium.dbf"
Select t_from, ;
cal_mth As calendar_month, ;
cal_yr As calendar_year, ;
substr(c1.Group,1,3) As BUS_LINE, ;
padr(Iif(isnumeric(Strextract(c1.Group,"|","|",1,1),""), ;
substr(c1.Group,1,4) + Strextract(c1.Group,"|","|",1,1), ;
strextract(c1.Group,"|","|",1,1)),10) As reinsurance_book, ;
padr(Strextract(c1.Group,"|","|",3,1),10) As Esc_Index, ;
padr(Iif(Left(c1.Group,3)="LRT",;
strextract(c1.Group,"|","|",5,1),;
strextract(c1.Group,"|","|",4,1)),10) As MAP_ELIG, ;
padr(Iif(Left(c1.Group,3)="LRT",;
strextract(Trim(c1.Group - "|"),"|","|",6,1), ;
strextract(Trim(c1.Group - "|"),"|","|",5,1)),10) As MAP_STAT, ;
SUM(ann_ben_if) As ann_ben_if ;
FROM (m.cube_output1) c1 ;
WHERE c1.t_from = 0 And ;
exists ( Select * From (m.cube_output2) c2 ;
Where c2.t_from = 0 And c1.Group = c2.Group ) ;
OR ;
exists ( Select * From (m.cube_output3) c3 ;
Where c3.t_from = 0 And c1.Group = c3.Group ) ;
group By t_from, calendar_month, calendar_year, ;
BUS_LINE, reinsurance_book, Esc_Index, MAP_ELIG, MAP_STAT ;
Into Cursor temp ;
Readwrite
**** Copy to a CSV file ****
COPY TO (m.cube2_output_path + "\IFRS_Split_Cashflows_2\run_" + m.scenario + ".csv") TYPE CSV