How to select first document in the same category

Is it possible to do this? I just want to select the first document with the same category? i.e :

Doct 1 : Category is Apple, Code is X15, quantity 3

Doct 2 : Category is Apple, Code is X15, quantity 5

Doct 3 : Cateogry is Orange, Code is X45, quantity 3

So the view should be:

Apple, X15

Orange, X45

Can this be done?

Subject: How to select first document in the same category

Are these categories fairly static? If so you could write an agent that would generate a viewEntryCollection which retains the Order and flag that first ViewEntry and clear the flag in any of the others if set. This could be hung on the PostSave event of a document as well, but be careful it could impact performance. Of course if the flag is already set on the document being saved don’t do anything. So it would only recompute the flag if the current document does not have the flag set.It would be a bit of a Kludge but I think it would work, and if the Categories are fairly static the performance hit would be minimal. The views should be pretty quick because the selection formula would be Flag = @True

I really stay away from a lot of calulations in view selection and columns. Do as much as you can at the document level, the user probably wont notice the brief pause as they save a document, but they will notice the long time it takes to re-build a view index. You could also create your own “Index” by setting the flag in the first document in the ViewEntryCollection to 1 and the second to 2 etc then be able to display the top 10 or 5 or whatever, you wanted.

Subject: RE: How to select first document in the same category

Tq Bill, I hoped I’ll be able to understand scripts in the next few weeks and than tried out your suggestion. Not so easy for me to comprehend yet, cause I tend to learn faster from sample code, than troubleshoot.

Really like your superb creative ideas!!!

Subject: How to select first document in the same category

There’s a “unique keys in index” option that limits the view to one document per key, but I don’t know how it chooses which one that will be. If you care, you will have to use a view selection that refers to a field on the document, and have an agent that sees to it that only one document in each set has that field set.

Subject: RE: How to select first document in the same category

I was thinking whether any one has tried using @subset or @IsMemberOf, or @List, take the first item?

Subject: RE: How to select first document in the same category

Imagine yourself in the situation of the “formula engine” that evaluates the formula. You are handed one document and you have to return true or false whether that document should be in the view. All the information you have is the fields in that one document. Can you tell whether it is “the first document in its category”?

The documents don’t come to you in any particular order – sorting comes after selection. You don’t have a way to store data to compare with later documents whether they have the same key. You have no way to modify an earlier decision. You can’t use @DbLookup or any other ways to fetch data from other documents (for performance reasons this is disallowed). You have to decide right now, yes or no, whether this document is in the view.

So no, you can’t use @Subset or other functions to select information from among multiple documents.

Subject: RE: How to select first document in the same category

Tqvm, Andre, really appreciates you took the time to explain vividly. I was having the same thought though, but was trying to know whether somehow someone manage to figure out some intelligent algorithm to solve this.

What I wanted was that to have a view lookup, and this view selection will take all documents. HOwever, I only need one document to be displayed, as the rest will be redundant. So that when I create another document, it will make use of this view for lookup in some of its fields. I know I can make use of category, but it tends to give me repeated data in the lookup field, when there is more than one document, and I have to use @Subset to display the first data only. HOwever, I know that in the field it actually contains more than one data, thus, I will have too many redundant data.

Again, thanks in advance.

Subject: RE: How to select first document in the same category

So it sounds like you’re using @DbLookup to this view, with the category name as a key, and reading data from another column, but you expect all documents with the same category to have the same data in this other column?

If your goal is just to avoid duplicate information, it still sounds to me like you could use “unique keys in index” view option. It doesn’t seem to matter which of the documents you retrieve data from if they all those with the same category contain the same data in that column.

Another thing you can do, is categorize two columns: your lookup key and also your data column that you read from. This will eliminate duplication.

And of course, you can always use @Unique afterwards to remove duplicates.