I would like to know how to write a query which helps me in getting the number of types of items bought by each customer. I would like to know this for and every customer.
schema
Customer
customer_name: String
did_order : [uid] (order)
Order
order.customer : uid (customer)
order.item : uid (item)
order.date
Item
item.name // examples :- apple or orange or pen …
each customer can place any number of orders, each order can have any number of items in it. There won’t be any duplicate items in each order and no facets or so to mention the quantity of the item purchased. Please assume the quantity to be one always. Finally, I would like to count the number of Distinct types of items bought by every customer. i.e :- please start the root with has(customer.name) instead of eq(customer.name,xxx).
P.S :- this is a scale-down of our use case which has abt 6 different types of entities and such. Please ignore any non-ideal schema declaration practices if any.
Thank you for the response. I have completed the tutorial(in fact many times already: sweat_smile:). My query is to get the “number of distinct items purchased by each customer”. The query posted by you gives me the list of all items purchased by each customer. But it is missing the aggregation(count) part and distinct part. To help you in visualizing my requirement better, I will give an example.
consider 2 customers who placed 2 orders each. lets say that customer A ordered item X and item Y in first order and item Y and item Z in other order, then the distinct items ordered by him are:- item X, item Y, and item Z and the count is 3.
Let’s say that customer B bought item Z in both of his orders, then the number of distinct items ordered by him is 1.
final response required is:-
A : 3
B: 1
I want to know how to do the same when I’m starting with has(customer.name) .i.e when i want to do it for everyone at once.
now, drifting away from layman terms. the challenge I’m facing is " when there are multiple nodes at the root level, I would like to be able to store query variables (uid_lists) whose scope is local. i.e separate uid_list for each node at root level "
Ah I see, yes thats beyond what the tutorial is covering. Sorry I misunderstood your question, it looked trivial. To do what you want, you need some kind of mapping of uids to aggregation.
While it’s easy to gather the informations for such a mapping with eg:
While computing total_items_by_Customer, Your query will count the total number of items ordered by each customer right. As @AugustHell said, i want to “count the number of distinct items bought by each customer”. In the example specified by me, the items ordered by the customer A are X, Y, Y, Z . So, the distinct items ordered by him are 3 (X,Y,Z).
Alice ordered items :- pencil,pen,quill
bob ordered item :- quill
Take away for me from this example :-
some way to create and use variable whose scope is limited i.e query variable(not value variable) whose scope is limited to each customer.
As far I can tell this wouldn’t be possible. Cuz they are multiple orders. We can count, but not “count unique”. However we can do this if we define a new block for each Customer. And that would have to be done manually. As you did in your first example.
Using the new @normalize behavior (coming soon), we can do something close to what you want. But it doesn’t take out the repeated ones.