拆分foxpro表

时间:2013-10-23 11:34:05

标签: sql foxpro

我确定这是一个简单的SQL,但我有一个表,其中包含X(目前为3个)级别的每个记录的多个记录。我基本上想把它复制到csv文件,每个级别一个。

我已经选择了SQL,我可以将其复制出来。我也可以选择获取文件中唯一级别的列表。我无法工作的是如何让foxpro循环遍历唯一级别并提供文件名并仅保存相关记录。

我正在使用scan来遍历唯一的记录,但显然我正在做的事情是错误的。

* identify the different LPG report levels
SELECT STREXTRACT(ALLTRIM(group),"|","|",3) as LPG_level FROM &lcFile GROUP BY LPG_level INTO CURSOR levels

TEXT to lcSql1 noshow textmerge pretext 15
   SELECT 
         LEFT(ALLTRIM(group),ATC("|",ALLTRIM(group))-1) as Sim, 
         STREXTRACT(ALLTRIM(group),"|","|",1) as Company, 
ENDTEXT

TEXT to lcSql2 noshow textmerge pretext 15
         time, 
         SUM(as) as Asset_Share_Stressed,
         SUM(as_us) as Asset_Share_Unstressed 
      FROM <<lcFile>> 
      GROUP BY Sim, 
         Company, 
         Fund, 
         LPG_level,
         Output_group, 
         time 
      ORDER BY sim asc,
            output_group asc
      INTO CURSOR bob
ENDTEXT


TEXT to lcSqlgroup2 noshow textmerge pretext 15    
     RIGHT(ALLTRIM(group),LEN(ALLTRIM(group)) - ATC("|",ALLTRIM(group),4)) as Output_group, 
ENDTEXT

    TEXT to lcSql_fund2 noshow textmerge pretext 15
        STREXTRACT(ALLTRIM(group),"|","|",2) as Fund, 
    ENDTEXT

    TEXT to lcSql_level noshow textmerge pretext 15
        STREXTRACT(ALLTRIM(group),"|","|",3) as LPG_level, 
    ENDTEXT

    &lcSql1 + &lcSql_fund2 + &lcSql_level + &lcSqlgroup2 + &lcSql2

    SELECT levels
    SCAN
        COPY TO output_path + lcFilename + levels.LPG_level for bob.LPG_Level = levels.LPG_Level

    endscan

3 个答案:

答案 0 :(得分:0)

尝试类似:

FOR curlevel = 1 TO numlevels
    outfile = 'file' + ALLTRIM(STR(curlevel)) + '.csv'

    TEXT TO contents
        blah blah
    ENDTEXT

    = STRTOFILE(contents, outfile)
ENDFOR

你必须调整一些东西,但这是一种可以使用的技术。

答案 1 :(得分:0)

我不知道为什么你有所有的text / endtext。您可以将SQL-Select构建为一个长语句...只需在每行末尾使用分号表示该语句在下一行继续(与C#不同;表示语句结束)。

无论如何,这种简化应该做你所拥有的

SELECT ;
      LEFT(ALLTRIM(group),ATC("|",ALLTRIM(group))-1) as Sim, ;
      STREXTRACT(ALLTRIM(group),"|","|",1) as Company, ;
      STREXTRACT(ALLTRIM(group),"|","|",2) as Fund, ;
      STREXTRACT(ALLTRIM(group),"|","|",3) as LPG_level, ;
      RIGHT(ALLTRIM(group),LEN(ALLTRIM(group)) - ATC("|",ALLTRIM(group),4)) as Output_group, ;
      time, ;
      SUM(as) as Asset_Share_Stressed, ;
      SUM(as_us) as Asset_Share_Unstressed ;
   FROM ;
      ( lcFile ) ;
   GROUP BY ;
      Sim, ;
      Company, ;
      Fund, ;
      LPG_level, ;
      Output_group, ;
      time ;
   ORDER BY ;
      sim asc,;
      output_group ASC ;
   INTO ;
      CURSOR bob

SELECT distinct LPG_Level ;
   FROM Bob ;
   INTO CURSOR C_TmpLevels

SELECT C_TmpLevels
SCAN 
   */ You might have to be careful if the LPG_Level has spaces or special characters
   */ that might cause problems in file name creation, but at your discretion.
   lcOutputFile = output_path + "LPG" + ALLTRIM( C_TmpLevels.LPG_Level ) + ".csv"
   SELECT Bob
   COPY TO ( lcOutputFile ) ;
      FOR LPG_Level = C_TmpLevels.LPG_Level ;
      TYPE csv
ENDSCAN 

在这种情况下,我只是构建了整个SQL查询并运行它...从这个结果中,我获得了不同的LPG_Level,因此它与您必须使用的结果集的结构完全匹配。注意在“FROM”子句中,我在括号中有(lcFile)。这告诉VFP查看表名的变量名,而不是名为“lcFile”的实际表作为文字。同样,当我将OUT复制到CSV文件时......复制到(lcOutputFile)。

宏“&amp;”可以是强大而有用的,但也可以咬你,特别是如果文件名路径中有空格...在这种情况下你是吐司...尝试习惯在这种情况下使用parens。

答案 2 :(得分:0)

感谢所有帮助。当我重新使用代码块时,我在另一个问题的建议中使用了text / endtext的东西,但TBH现在有点矫枉过正,所以我可能会在某些时候清理它。

我确实得到了团队中其他知道某些SQL的人的帮助,但该解决方案看起来非常类似于DRapp。

在下面粘贴作为另一个时间的提醒或帮助其他人。

FOR i=1 TO lnCnt
lcFile = LOWER(output_path + laFiles[i,1])
lcFilename = LEFT(laFiles[i,1],ATC("~main4",laFiles[i,1])-1)

IF file(lcFile) = .F. then
    ERROR "File " + lcFile + " does not exist"
ENDIF

* Status window, tell user which file being processed
WAIT WINDOW (lcFile + ". File: " + ALLTRIM(STR(i)) + " of " + ALLTRIM(STR(lnCnt))) nowait

* identify the different LPG report levels
SELECT STREXTRACT(ALLTRIM(group),"|","|",3) as LPG_level FROM &lcFile GROUP BY LPG_level INTO CURSOR levels

* Build up the select string, starting with the sim number and company
TEXT to lcSql1 noshow textmerge pretext 15
   SELECT 
         LEFT(ALLTRIM(group),ATC("|",ALLTRIM(group))-1) as Sim, 
         STREXTRACT(ALLTRIM(group),"|","|",1) as Company, 
ENDTEXT

* Add the fund
TEXT to lcSql_fund2 noshow textmerge pretext 15
    STREXTRACT(ALLTRIM(group),"|","|",2) as Fund, 
ENDTEXT

* Add the Output group
TEXT to lcSqlgroup2 noshow textmerge pretext 15    
     RIGHT(ALLTRIM(group),LEN(ALLTRIM(group)) - ATC("|",ALLTRIM(group),4)) as Output_group, 
ENDTEXT

* Not actually required but helps clarify the output groups, the report level (e.g. tax-status, LoB, vclass)
TEXT to lcSql_level noshow textmerge pretext 15
    STREXTRACT(ALLTRIM(group),"|","|",3) as LPG_level, 
ENDTEXT

* Add the rest of the select, with the fields and their output names, the fields to group on and the sort order 
TEXT to lcSql2 noshow textmerge pretext 15
         time, 
         SUM(as) as Asset_Share_Stressed,
         SUM(as_us) as Asset_Share_Unstressed, 
         SUM(cogao) as CoGAO, 
         SUM(cog) as CoG, 
         SUM(cope) as CoPE, 
         SUM(cos) as CoS, 
         SUM(cope_wluk) as CoPE_WLUK, 
         SUM(copd_gteed) as CoPD_Gteed, 
         SUM(copd_other) as CoPD_Other,
         SUM(fprl_resid) as FPRL_resid,
         SUM(fprlcosadj) as FPRL_CoS_Adj,
         SUM(woc_res) as WOC_Reserve,
         SUM(bel) as BEL, 
         SUM(sht) as SH_Transfers, 
         SUM(pol_count) as Pol_Count 
      FROM <<lcFile>> 
      GROUP BY Sim, 
         Company, 
         Fund, 
         LPG_level,
         Output_group, 
         time 
      ORDER BY sim asc,
            output_group asc
      INTO CURSOR bob
ENDTEXT

* These are the variables which were created from the text above. This line combines them and runs them as a select statement
&lcSql1 + &lcSql_fund2 + &lcSql_level + &lcSqlgroup2 + &lcSql2

* Loop over the available report levels (previously exported to the cursor called levels
SELECT levels
SCAN
    * From bob (the cursor created above which has everything in it) for the current report level select out the relevant records
    * and save them to the original filename with the level appended in CSV format
    SELECT * FROM bob WHERE bob.LPG_Level = levels.LPG_Level INTO CURSOR temp
    COPY TO output_path + lcFilename + "_" + ALLTRIM(levels.LPG_Level) + ".csv" TYPE CSV
ENDSCAN


* Next file in list
NEXT i