0

I have data like so:

[Equipment] [DateTimeIn]
T-0001B    2017-05-05T00:00:00:00.000
T-0001B    2017-06-05T15:00:00:00.000
T-0503A    2017-03-05T13:00:00:00.000

And am trying to get an output like this:

T-0001B    2017-06-05T15:00:00:00.000
T-0503A    2017-03-05T13:00:00:00.000

The "table" is a list of transfers for all equipment with a datefield of when the transfer took place.

I'm new to CouchDB/PouchDB, having been in SQL land for 15 or so years. I've searched this and run across quite a few examples of how to group by fields (keys or partial keys), and how to summerize them. Because there does not seem to be any "Reduce: __max" method, how would I go about getting the "latest row" for each equipment as in the above example? My real data consists of about 5000 docs with 1000 unique equipment ids.

I could see this being used in apps to show "latest comment on each thread" or "last reply on XXXXX" per thread (for forum style apps). In my case I'm interested in the latest row for each equipment so I can then query JUST those rows to further filter them to determine how many are on a particular job, or how many are still in transit..etc (other fields).

Any ideas?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Suthern
  • 45
  • 8
  • You have to build your own reduce function. Basically, your reduce function should find the max value and return it. Then, you query your view with the parameters ?reduce=true&group=true – Alexis Côté Aug 28 '17 at 18:45
  • I'll give that a shot and report back. I see there are some MIN and MAX-like examples at https://stackoverflow.com/questions/38779998/how-to-simulating-the-aggregate-functions-avg-sum-max-min-and-count-on-pouch. – Suthern Aug 28 '17 at 19:19
  • I've decided to go another route and add a IS_LATEST flag to the most recent transfers. This might not be the best from a design perspective as it requires updating multiple documents, but it does make the queries much easier to manage. And because map/reduce functions are so tricky, I'm most likely going to stick with this route. – Suthern Aug 29 '17 at 19:12

0 Answers0