FoxPro 9.0使用聚合连接表

时间:2012-12-14 21:26:34

标签: foxpro

我想汇总我的决赛桌,不知道如何解决加盟部分。我有一个Do While循环遍历我的表contract.dbf,它将每一行传递给一个过程。此过程test33使用此信息并从其他表中选择所需的数据。我想要的聚合是每个循环的所有结果,所以如果contracts.dbf中有101行,那么join.dbf将是101列宽。

DELETE FILES *.tmp RECYCLE
SELECT distinct depot_nr FROM bs_case;
INTO table contracts.tmp

SELECT RECNO() as rownum,;
    depot_nr as depot_nr;
FROM contracts.tmp
NbContracts =RECCOUNT()
COPY TO test3.dbf
CLOSE TABLES

counter = 1

DO WHILE counter < NbContracts
    SELECT depot_nr as depot_nr;
    WHERE rownum = counter FROM test3
    test33(depot_nr, counter)
    counter = counter + 1
ENDDO
CLOSE TABLES 

PROCEDURE test33(depot_nr_in, NbofTimes)
use bs_case alias bs
SELECT  Depot_nr                as depot_nr,;
        Psres3pcgb              as psres3pcgb;
    WHERE Depot_nr = depot_nr_in FROM bs INTO TABLE toJoin.tmp

DO CASE 
    CASE NbofTimes = 1
        SELECT * FROM toJoin.tmp 
        COPY TO joining.dbf
    CASE NbofTimes = NbContracts
        ?counter
        SELECT * FROM bsP.tmp as one LEFT JOIN joining.dbf as aggregated; && ERROR HERE
        ON (one.depot_nr = aggregated.depot_nr) into table joining.dbf
        CLOSE TABLES
        ENDPROC
    Otherwise 
        SELECT * FROM toJoin.tmp as one LEFT JOIN joining.dbf as aggregated;   && ERROR HERE
        ON (one.depot_nr = aggregated.depot_nr) into table joining.dbf
        CLOSE TABLES
ENDCASE
CLOSE TABLES
CLOSE DATABASES
ENDPROC

数据看起来像

bs_case
===================================
depot_nr      Psres3pcgb
22              123
31              222
22              345
32              444
23              222
22              222

contracts.dbf
===================================
22
31
32
23

我的过程将contracts.dbf中的每个值作为参数。这是通过do while循环完成的。

我希望最终结果是每次运行test33过程的表。 E.g。

Loop 1
===============
   22
the result

Loop2
==============
    22          31
test33(22)   test33(31)

Loop3
==============
    22          31             32
test33(22)   test33(31)     test33(32)

Loop4
==============
    22          31             32              23
test33(22)   test33(31)     test33(32)     test33(23)

test33(##)的每个结果都是一列值。我希望这可以更清楚地了解我要做的事情

1 个答案:

答案 0 :(得分:1)

除非你需要存储未来运行的结果,否则你永远不需要SELECT INTO TABLE或COPY TO。

我觉得你根本不需要子程序或循环。能否请您展示一些样本数据和所需的结果。使用CREATE CURSOR和INSERT INTO显示示例数据,因此任何想要帮助的人都可以复制这些行并创建测试数据。