英文:
Do While loop logic
问题
I am trying to load data from multiple qvd files and then save it. However, I am facing issues with the loop logic, as it's creating 12 TMP tables but only loading from the first. Any tips on how to fix this?
英文:
I am trying to load data from multiple qvd:s and then save it like
LET Start = AddMonths(makedate(year(Today()), 1, 1),0);
LET End = AddMonths(makedate(year(Today()), 12, 31),0);
do while Start <= End
let YearMM = Year(date('$(Start)','YYYY-MM-DD'))*100+Month(date('$(Start)','YYYY-MM-DD'));
NoConcatenate
TMP:
LOAD
Field1,
Field2,
Field3
from [path/File_$(YearMM).qvd]
(qvd);
left Join(TMP)
Load
Field1,
Field2
FROM [Path/file2.qvd]
(qvd);
left join(TMP)
load distinct
Field1
max(YearMM) as YearMM,
1 as MaxYearMM
resident TMP
group by Field1;
Drop Table TMP;
LET Start = date(AddMonths('$(Start)',1),'YYYY-MM-DD');
LOOP
NoConcatenate
EndTable:
load
*
resident TMP
where MaxYearMM=1;
store EndTable into [Path/endfile.qvd] (qvd);
But i can not get the loop logic to work so at the end it would go correctly. Currently it gives me 12 TMP tables and loads only from the first. Any tips how to fix?
答案1
得分: 1
以下是您要求的翻译内容:
看起来您正在循环中将这些表连接到[TMP]
表中,但在循环结束之前立即将其删除?我认为您需要的是这样的东西:
Let Start = AddMonths(MakeDate(Year(Today()), 1, 1), 0);
Let End = AddMonths(MakeDate(Year(Today()), 12, 31), 0);
// 更改 #1
[EndTable Load]: Load * Inline [Field1];
Do While Start <= End
Let YearMM = Year(Date('$(Start)', 'YYYY-MM-DD')) * 100 + Month(Date('$(Start)', 'YYYY-MM-DD'));
[TMP]:
NoConcatenate Load
[Field1]
, [Field2]
, [Field3]
From [$(vStorePath)/File_$(YearMM).qvd] (QVD);
Left Join([TMP])
Load
[Field1]
, [Field2]
From [$(vStorePath)/file2.qvd] (QVD);
Left Join([TMP])
Load Distinct
[Field1]
, Max([YearMM]) as [YearMM]
, 1 as [MaxYearMM]
Resident [TMP]
Group By [Field1]
;
// 更改 #2
Concatenate ([EndTable Load]) Load * Resident [TMP];
Drop Table [TMP];
Let Start = Date(AddMonths('$(Start)', 1), 'YYYY-MM-DD');
Loop
[EndTable]:
NoConcatenate Load *
Resident [EndTable Load] // <--------- // 更改 #3
Where [MaxYearMM] = 1
;
// 更改 #4
Drop Table [EndTable Load];
Store [EndTable] Into [$(vStorePath)/endfile.qvd] (QVD);
除了一些格式上的更改外,以下是我在脚本中所做的更改:
更改 #1
在循环开始之前,我添加了一个名为[EndTable Load]
的新空表。这是因为我们将需要一个表来累积每次循环遍历这些文件的数据。似乎您尝试使用[TMP]
表来实现这一目标,但在尝试确定何时使用NoConcatenate
等内容时可能会遇到困难。
更改 #2
在循环结束之前,我们将来自[TMP]
表的数据连接到我们的新[EndTable Load]
表中。这是如何在循环迭代之间实现累积的方式。
更改 #3
在循环完成后,我们使用Resident
将[EndTable Load]
转为一个新表[EndTable]
,其中包括我们的Where
子句。
更改 #4
最后,现在我们有了最终的表[EndTable]
,我们可以删除先前的表[EndTable Load]
,因为我们现在不再需要它。
英文:
It looks like you're joining those tables together in your loop into the [TMP]
table but then just immediately dropping it before the end of the loop?
I think what you need is something like this:
Let Start = AddMonths(MakeDate(Year(Today()), 1, 1), 0);
Let End = AddMonths(MakeDate(Year(Today()), 12, 31), 0);
// CHANGE #1
[EndTable Load]: Load * Inline [Field1];
Do While Start <= End
Let YearMM = Year(Date('$(Start)', 'YYYY-MM-DD')) * 100 + Month(Date('$(Start)', 'YYYY-MM-DD'));
[TMP]:
NoConcatenate Load
[Field1]
, [Field2]
, [Field3]
From [$(vStorePath)/File_$(YearMM).qvd] (QVD);
Left Join([TMP])
Load
[Field1]
, [Field2]
From [$(vStorePath)/file2.qvd] (QVD);
Left Join([TMP])
Load Distinct
[Field1]
, Max([YearMM]) as [YearMM]
, 1 as [MaxYearMM]
Resident [TMP]
Group By [Field1]
;
// CHANGE #2
Concatenate ([EndTable Load]) Load * Resident [TMP];
Drop Table [TMP];
Let Start = Date(AddMonths('$(Start)', 1), 'YYYY-MM-DD');
Loop
[EndTable]:
NoConcatenate Load *
Resident [EndTable Load] // <--------- // CHANGE #3
Where [MaxYearMM] = 1
;
// CHANGE #4
Drop Table [EndTable Load];
Store [EndTable] Into [$(vStorePath)/endfile.qvd] (QVD);
Besides some formatting, here are the changes I made in the script:
Change #1
I added a new empty table called [EndTable Load]
before the loop starts. This is because we'll need a table that can accumulate the data from each loop through those files. It seems like you were trying to achieve that with just the [TMP]
table but then you can get stuck trying to figure out when to use NoConcatenate
and stuff.
Change #2
Before the end of the loop, we concatenate the data from the [TMP]
table into our new [EndTable Load]
table. This is how that accumulation happens across the loop iterations.
Change #3
After the loop finishes, we Resident [EndTable Load]
to get a new table called [EndTable]
that includes our Where clause.
Change #4
Finally, now that we have our final table, [EndTable]
, we can delete the previous one, [EndTable Load]
, since we now no longer need it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论