Sunday, November 20, 2022

Belated discovery about indices and ClientDataSets

In my blogs database program, I wanted that one query would display a list of all tags, ordered by the number of appearances and by name. The number of appearances has to have descending sort, so that the first tag appearing is the most popular, whereas the last tag is the least popular. On the other hand, there are many tags that appear only once and it would be nice if these tags could be sorted in alphabetical order. In terms of SQL, this would be

select tags.name, count (*) as id from tags inner join tag2entry on tag2entry.tag = tags.id group by 1 order by 2 desc, 1
but because of the way that the indices are defined in Delphi, I am using the AddIndex call in the following manner:
qTags.addindex ('idx', 'id', [ixdescending], '', '', 0);
Whilst this gives me a descending index on 'id', it doesn't explain how to have one field sorted in descending order and one in ascending order. Maybe I have never come across this situation before, previously having allowed the user to sort by one field only, either ascending or descending.

After poking around a little, I found the answer here; this involves one of the mysterious parameters that are passed as empty strings to AddIndex. It turns out that one leaves the options set empty, then adds in the first string the name(s) of the field(s) that are to be sorted in descending order (the name of this parameter is 'DescFields', making it more explicit). The second string parameter is called 'CaseInsFields', controlling which fields are sorted by case and which not. In Hebrew this doesn't make any sense (which is probably why I've never come across this problem before), but in the blogs program, it would make 'At' come before 'as'. The command that I am using is
qTags.addindex ('idx', 'id;name', [], 'id', '', 0);


This day in history:

Blog #DateTitleTags
64920/11/2013More song festivalMIDI, Kibbutz, Song writing
65020/11/2013Stack OverflowProgramming

No comments: