简体   繁体   中英

Double records when exporting data to CSV

I have created a method where I export data to a CSV file, I have created the following code:

public void export()
{

    permission = new FileIOPermission(fileNameValue, #io_WRITE);
    permission.assert();

    inFile = new CommaTextIo(fileNameValue, #io_WRITE);
    inFile.inFieldDelimiter(";");
    inFile.inRecordDelimiter("\n");

    while (inFile.status() == IO_Status::Ok)

    while select inventTrans
    where inventTrans.StatusIssue == StatusIssue::ReservPhysical
    join inventDim
    where inventDim.InventLocationId == inventLocationId
    join inventTransOrg
    where inventTransOrg.RecId == inventTrans.InventTransOrg

    {

     con = conNull();

       con = conIns(con, 1, inventTransOrg.inventTransId);
       con = conIns(con, 2, inventLocationId);
       con = conIns(con, 3, inventTrans.Qty);

        inFile.writeExp(con);
}
}

This creates a CSV file with 3 columns, but alot of double records (almost 100) the actual data does not contain 100 double records. Any suggestions how I can rebuild the export to avoid the double records?

Your joins will produce 'duplicates' as eg your join with inventDim via inventLocationId results probably in more than one record so your inventTrans is 'repeated' once for each of those records.
Reevaluate your query to solve your issue; one thing you probably want to change is linking to InventDim via InventDimId .

Your problem has nothing to do with the CSV exporting code as you can easily see if you replace the CSV file actions with an output to the infolog .

The mistake is that you not joining Inventdim table with any other table. So it will result in cross join. So change your while statement to include that. My suggestion will be to add field list also as InventTrans will have lot of fields and to have better performance it is advised to have field list for select statements. You can try using below statement too.

While select inventTransId from inventTransOrg join Qty from inventTrans where inventTrans.InventTransOrg = inventTransOrg.RecId join inventLocationId from inventDim where inventDim.InventDimId == inventTrans.InventDimId && inventDim.InventLocationId == inventLocationId

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM