This Question is Assumed Answered

1 "correct" answer available (5 pts) 15 "helpful" answers available (3 pts)
1 Replies Last post: Jul 18, 2008 2:13 PM by dmccrae

Return unique values from a RecordSet

Jul 18, 2008 8:28 AM

Click to view CalVic's profile Level 1 CalVic 7 posts since
Jun 27, 2008
I have a CsvRecordSet and I am trying to filter the data using dropdown menus, so I would like to grab the list of unique values. Is there any way that I could return the list of unique values from a RecordSet? Thanks.
Click to view dmccrae's profile Curl dmccrae 22 posts since
Oct 10, 2007
1. Re: Return unique values from a RecordSet Jul 18, 2008 5:04 PM
A common approach is to iterate over the records, and collect and organize the unique values.

{define-proc public {unique-values
                        rs:RecordSet,
                        f:RecordField
                    }:Array
    || field info
    def name = f.name
    def domain = f.domain
    || collect unique values
    def vals = {Set}
    {for r in rs do
        {vals.insert r[name]}}
    || sort according to domain
    def sorted-vals = {Array efficient-size = vals.size}
    {for val in vals do
        {sorted-vals.append val}}
    {sorted-vals.sort comparison-proc =
        {proc {x:any, y:any}:bool
            {return {domain.compare x, y} <= 0}}}
    || result
    {return sorted-vals}    
}


Of course, you could do this in SQL, with 'SELECT DISTINCT', if you're using the Curl Data Kit Project which makes the SQLite database engine available for use in Curl applications.

I don't mean to suggest that is necessary just for collecting unique values, but the more complex the data processing (aggregation, joins, indexes, etc), the more code you can save by using SQL instead.