In a table you have product_id and order_created_date which is a datetimestamp field. Now we required output as below. For each product_id how many orders are created today, how many orders are created last 5 days, how many orders are created last 30 days, total no of orders created till today
Hi @siri - this isn't really a graph-type problem, but we can solve it using a similar technique as we would use in SQL. I don't have your data, so I mocked something up using a random number from 0-100 - this represents the number of days since the order was created. I then use case statements to generate "flags" that represent your time buckets. In my example I use 5 days and 10 days. We can then simply sum those flags. Something like this:
match (n)
with apoc.convert.toInteger(100 * rand()) as d
with case when d<=5 then 1 else 0 end as isIn5Days,
case when d<=10 then 1 else 0 end as isIn10Days
return sum(isIn5Days), sum(isIn10Days)
Sample Query for 30days i tried is as below and similar i can try for last 5 days. My question now is how to club both the outputs to come in single output
WITH duration({days:30}) AS duration
MATCH(p:product) where date(datetime(p.order_created_date))>date()-duration
RETURN Product_Id,size(collect(date(datetime(p.order_created_date)))) as last30days
@siri I was giving you a principle to work from. I don't have your data, so I made some up. Instead of making data up like I did, you would write a query that returned the product ID and the number of days between the order date and today. That's your starting point. Then, you can use the CASE expressions like I did to take the number of days between the order date and today and compute the "flags" (just like I did). Finally, return the product_id and the sums of the flags.
If you need some more details - give us some sample data to work with (i.e. - give us some cypher commands that create the data) and show us where you're getting stuck.
@siri I could write the query for you, but I'd rather help you learn how to write the query. Let's piece this together.
First, write a query that will return the product ID and order created date - that is pretty simple.
Next - add to that query to return how many days ago the order was created. Hint: See https://neo4j.com/docs/cypher-manual/current/functions/temporal/ to figure out the current date. You now have a query that returns product_id and number_of_days_since_order_was_created
Next, use case expression to create the 1/0 flags for the "buckets" Your buckets will be 0-1 days, 0-7 days, 0-30 days.
Then, as I showed you, query the product_id and sum up the flags to get the total. You could use another aggregate function to get the COUNT (hint hint) of total orders created.
Take a stab at it. If you can't figure it out, show us what you tried and where you ran into a problem, then I'll be happy to help you out further. But for now, the ball is in your court.
Second. when I fix the syntax error and run the query, I get this result:
(no changes, no records)
Probably because I don't have your data. If you wanted to provide your data in a format that I can use (e.g. Cypher statements that create the data) and show us the results you are getting and the expected results, maybe someone will help.
More directly: you're asking a question on a public forum and not making it easy for anyone to help you.