select distinct

1 view
Skip to first unread message

Peter De Keer

unread,
Jun 2, 2009, 12:56:52 PM6/2/09
to dovetaildb-discuss
I know I'm asking questions not only needed for dovetail, but how
could I do this?

SQL SELECT DISTINCT function FROM Jobs

psch...@gmail.com

unread,
Jun 3, 2009, 9:24:29 AM6/3/09
to dovetaildb-discuss
The plain old javascript way to do it is to set keys on an object:

==== BEGIN JS ====
// supposing <jobs> is an array of objcts:
var functions = {};
for(var i=0; i<jobs.length; i++) {
functions[jobs[i].function]=1;
}
for (var f in functions) {
// will iterate only once for each distinct value
}
==== END JS ====

You can also do it server-side with DovetailDB's map/reduce:

==== BEGIN SERVER-SIDE JS ====
function getJobFunction(job) {
var functions={};
functions[job.function] = 1;
return functions;
}
function mergeFunctions(functions1, functions2) {
for(var k2 in functions2) {
functions1[k1] = 1;
}
return functions1;
}
==== END SERVER-SIDE JS ====


And calling on the client side like so:

==== BEGIN JS ====
dovetail.query('people', {}, {map:'getJobFunction',
reduce:'mergeFunctions'}, function(functions) {
for(var f in functions) {
// will iterate only once for each distinct value
}
});
==== END JS ====


Hope this helps!
Phil
Reply all
Reply to author
Forward
0 new messages