Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't provide many details about your use case so I'll try to give you some hints:</p> <ol> <li><p>"A lot of data" suggests that you might have it from a SQL backend. Using a 'SELECT DISTINCT...' or 'SELECT ... GROUP BY BRANCH_ID' (or similar syntax depending on what SQL backend do you) will give the desired result with ease and speed. Please confirm and I'll give you more details.</p></li> <li><p>As the others said a simple 'clone' wouldn't work. The most simpler (and perhaps quicker) sollution, assuming that ussually the brances are few in number WRT to data is to have an index outside of your dataset. If really you want to filter your original data then add a status field (eg boolean) on your data and put a flag (eg. 'True') on the first occurence.</p></li> </ol> <p>PseudoCode: (Let's asume that: your ClientDataSet is cds1 your cds1 have a status field cds1Status (boolean) - this is optional, needed only if you want to sort/filter/search the cds1 you have a lIndex which is a TStringList)</p> <pre><code>lIndex.Clear; lIndex.Sorted:=True; with cds1 do try DisableControls; First; while not Eof do //scan the dataset begin cVal:=cds1Branch_ID.AsString; Edit; //we anyway update the Status field if lIndex.Find(cVal, nDummy) then //nDummy - we don't use it. begin //already in index cds1Status.AsBoolean:=False; //we say here "No, isn't the 1st occurence" end else begin //Not found! - Well, let's add it... lIndex.Append(cVal); //update the index cds1Status.AsBoolean:=True; //mark the first occurence end; Post; //save the changes in the status field Next; end; //scan finally EnableControls; //housekeeping end; </code></pre> <p>//WARNING! - NOT tested. I wrote it from my head but I think that you got the idea...</p> <p>...Depending on what you try to accomplish (which would be the best thing that you might share with us) and what selectivity do you have on BRANCH_ID perhaps the Status engine isn't needed at all. If you have a very low selectivity on that field (selectivity = no. of unique values / no. of records) perhaps it's much faster to have a new dataset and copy there only the unique values rather than putting each record of the original cds in Edit + Post states. (Changing dataset states are costly operations. Especially if your cds is linked to a remote data storage - ie. a server).</p> <p>hth,</p> <p>PS: My sollution is intended to be mostly simple. Also you can test with lIndex.Sorted:=False and use lIndex.IndexOf instead of Find. In some (rare) cases is better. Depends on your data. If you want to complicate the things and the speed is really a concern you can implement a full-blown BTree index to do your searces (libraries available). Also you can use the index engine of CDS and index the BRANCH_ID and do many 'Locate' on a clone but because your selectivity is clearly &lt; 1 scaning the cds's entire index theorethically should be slower that a scan on a unique index, especially if your custom-made index is tailored to your data type, structure, distribuition etc.</p> <p>just my2c</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload