I'm sorry that I didn't understand the program correctly at the beginning.
I think extracting distinct values across over all 8 fields (and count of each value) is the first target.
To get values only (not counts), SQL
select distinct f1 from... and then selecting distinct values from them again would be the simplest method. And this takes quite long time.
As the whole number of distinct values does not seem large for now, I think we can count it in Lazarus.
valcounts:= TFPGMap<integer, integer>.Create;
valcounts.sorted := True;
with sqlquery do begin
sql.text := 'select * from table1';
open;
first;
while not eof
for (f in fields) do begin
v := f.asinteger;
if valcounts.find(v, j)
then inc(valcounts.data[j])
else valcounts.add(v, 1);
end;
next;
end;
close;
end;
Conceptually this will give all the distinct values and counts.
But in reality, simply opening the sqlquery will stop PC (for several hours at least).
So, I think of opening subsets of table several times, using limit and offset, for example,
select ... limit 10000 offset 10000
I hope this approach provides some manageable points.
Once you have the values here, then you may add a new table with these values, and triggers Zvoni provided.