2013-10-23 40 views
0

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

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

我正在使用扫描来遍历独特的记录,但显然我在做什么,然后是错误的。

* 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 

回答

0

我不知道为什么你有所有的文本/ endtext。您可以将SQL-Select构建为一个long语句......只需在每行末尾使用分号来指示语句在下一行继续(与在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)。

宏“&”可以是强大的和有用的,但也可以咬你,尤其是如果文件名路径中有一个空间...你在这种情况下烤面包...尝试习惯使用parens在这种情况下。

0

试着这么做:

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

    TEXT TO contents 
     blah blah 
    ENDTEXT 

    = STRTOFILE(contents, outfile) 
ENDFOR 

你必须要调整的事情,但是这是使用的技术。

0

感谢您的帮助。当我重新使用代码块时,我从另一个问题的文章/ 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