0

I want to create a view in cb that will show some data based on date-range input.

My question is how can I distinct field from the data? Here is my map code:

function (doc, meta) {
  if(!doc.clipGeneration.fromCache){
    var eiid = doc.clipGeneration.batchId;
    if (eiid == null){
        eiid = "API";
    }
    if (  !(doc.clipGeneration.externalId.indexOf("API_INTERVAL_TEST")>=0) )   {
    emit([doc.clipGeneration.clipStyle, eiid,doc.eventOccurenceTimeStamp[0], doc.eventOccurenceTimeStamp[1], doc.eventOccurenceTimeStamp[2],doc.eventOccurenceTimeStamp[3]], doc.insertRawTimestamp);
        emit([doc.clipGeneration.clipStyle, "ALL",doc.eventOccurenceTimeStamp[0], doc.eventOccurenceTimeStamp[1], doc.eventOccurenceTimeStamp[2],doc.eventOccurenceTimeStamp[3]], doc.insertRawTimestamp);
    emit([doc.eventOccurenceTimeStamp[0], doc.eventOccurenceTimeStamp[1], doc.eventOccurenceTimeStamp[2],doc.clipGeneration.clipStyle], doc.insertRawTimestamp);
    }
  }
}

The reduce is _count, and the filters for example are:

startkey: [2015,1,1,null]

endkey: [2015,1,31,"\uffff"]

The output is as expected by the date-range:

{"key":[2015,1,11,"5001188"],"value":1},
{"key":[2015,1,12,"100022"],"value":5},
{"key":[2015,1,12,"155"],"value":11},
{"key":[2015,1,13,"100022"],"value":9},
{"key":[2015,1,13,"155"],"value":6},
{"key":[2015,1,13,"5001159"],"value":1},
{"key":[2015,1,13,"5001190"],"value":3},
{"key":[2015,1,14,"100022"],"value":12},
{"key":[2015,1,14,"5001194"],"value":1},
{"key":[2015,1,15,"100022"],"value":11},
{"key":[2015,1,16,"100022"],"value":10},
{"key":[2015,1,18,"100022"],"value":8},
{"key":[2015,1,18,"5001096"],"value":6},
{"key":[2015,1,18,"5001194"],"value":3}

But as you can see "100022" repeat many times, how can I make somthing like distinct in sql, so it will be shown only once?

Thanks

Elad
  • 891
  • 5
  • 15

1 Answers1

0

There is no way to actually make ONLY the ID field distrinct. You can reduce by key, but only the whole key - so [date, id] in your case. You can do this in N1QL, because it has an actual DISTINCT keyword just like SQL, but that's still in developer preview at the moment.

David Ostrovsky
  • 2,461
  • 12
  • 13