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.