我们的订单包括" caused_order"从订单到订单的边缘,因为朋友可以推荐其他朋友进行购买。我们通过我们为朋友生成的链接知道订单ID 42导致订单ID 47,因此我们创建了一个" caused_order"两个Order顶点之间的边缘。
我们正在寻找能够产生最多转介业务的人。现在我们只是在C#中循环并找出它,因为我们的数据集相对较小。但是我想弄清楚是否有办法使用Traverse SQL来实现这一目标。
我遇到的问题是获得每个原始订单ID的准确计数/总和。
考虑以下情况:
第42号令导致其他四项订单,包括第47号订单。第47号订单导致另外2个订单。与42或47无关的第51号命令导致了3个命令。
我可以运行以下SQL来获取此特定{ProductId}的最佳引荐来源:
select in_caused_order[0].id as OrderID, count(*) as ReferCount, sum(amount) as ReferSum
from ( traverse out('caused_order') from Order )
where out_includes.id = '{ProductId}' and $depth >= 1
group by in_caused_order[0].id
编辑:架构比这复杂一点,我只是包含了out_includes WHERE子句,以显示对订单进行了一些过滤。但它有点像:
Product(V) <-- includes(E) <-- Order(V) --> caused_order(E) --> Order(V)
(the Order vertex has "amount" as a property, which stores the money spent and is being SUM'd in the SELECT, along with a few fields like date which aren't important)
但这会产生类似的结果:
OrderID | ReferCount | ReferSum
42 | 4 | 525
47 | 2 | 130
51 | 3 | 250
除此之外不太正确,是吗?因为第42号订单在技术上也导致了47个订单。所以我们希望看到类似的内容:
OrderID | ReferCount | ReferSum | ExtendedCount | ExtendedSum
42 | 4 | 525 | 2 | 130
47 | 2 | 130 | 0 | 0
51 | 3 | 250 | 0 | 0
我认识到这两个&#34;扩展&#34; count / sum列可能很棘手。我们可能必须运行查询两次,一次使用$ depth = 1,再次运行$ depth&gt; 1,然后在C#中汇总这两个查询的结果,这很好。
但我甚至无法弄清楚如何正确计算总体总数。第一步甚至可以看到:
OrderID | ReferCount | ReferSum
42 | 6 | 635 <-- includes its 4 orders + 47's 2 orders
47 | 2 | 130
51 | 3 | 250
由于这可能是n级深度,但我不能在SQL中以某种方式执行in_caused_order.in_caused_order.in_caused_order,我不知道会有多少深度。第83号命令可能由第47号命令引起,第105号命令可由第83号命令引起,依此类推。
非常感谢任何帮助。或许答案是,Traverse无法解决这个问题,我们必须完全解决其他问题。
答案 0 :(得分:0)
我正在尝试你的用例,以下是我的测试数据:
create class caused_order extends e
create class Order extends v
create property Order.id integer
create property Order.amount integer
begin
create vertex Order set id=1 ,amount=1
create vertex Order set id=2 ,amount=5
create vertex Order set id=3 ,amount=11
create vertex Order set id=4 ,amount=23
create vertex Order set id=5 ,amount=31
create vertex Order set id=6 ,amount=49
create vertex Order set id=7 ,amount=4
create vertex Order set id=8 ,amount=74
create vertex Order set id=9 ,amount=87
create edge caused_order from (select from Order where id=1) to (select from Order where id=2)
create edge caused_order from (select from Order where id=1) to (select from Order where id=3)
create edge caused_order from (select from Order where id=2) to (select from Order where id=4)
create edge caused_order from (select from Order where id=2) to (select from Order where id=5)
create edge caused_order from (select from Order where id=6) to (select from Order where id=7)
create edge caused_order from (select from Order where id=6) to (select from Order where id=8)
commit retry 20
然后我写了这两个查询来显示相对于referSum和ReferCount的订单。
第一个包括计数中的头部顺序:
select id as OrderID, $a[0].Amount as ReferSum, $a[0].Count as ReferCount from Order
let $a=(select sum(amount) as Amount, count(*) as Count from (traverse out('caused_order') from $parent.$current) group by Amount)
第二个,不包括头部:
select id as OrderID, $a[0].Amount as ReferSum, $a[0].Count as ReferCount from Order
let $a=(select sum(amount) as Amount, count(*) as Count from (select from (traverse out('caused_order') from $parent.$current) where $depth>=1) group by Amount)
修改强> 我已将此添加到我的数据中:
create class includes extends E
create class Product extends V
create property Product.id Integer
create vertex Product set id = 101
create vertex Product set id = 102
create vertex Product set id = 103
create vertex Product set id = 104
create edge includes from (select from Order where id=1) to (select from Product where id=101)
create edge includes from (select from Order where id=2) to (select from Product where id=102)
create edge includes from (select from Order where id=3) to (select from Product where id=103)
create edge includes from (select from Order where id=4) to (select from Product where id=104)
create edge includes from (select from Order where id=5) to (select from Product where id=101)
create edge includes from (select from Order where id=6) to (select from Product where id=102)
create edge includes from (select from Order where id=7) to (select from Product where id=103)
create edge includes from (select from Order where id=8) to (select from Product where id=104)
create edge includes from (select from Order where id=9) to (select from Product where id=101)
create edge includes from (select from Order where id=1) to (select from Product where id=102)
create edge includes from (select from Order where id=1) to (select from Product where id=103)
create edge includes from (select from Order where id=2) to (select from Product where id=104)
这些是经过修改的查询(在遍历和while out('includes').id contains {prodID_number}
中添加了where out('includes').id contains {prodID_number}
:
select id as OrderID, $a[0].Amount as ReferSum, $a[0].Count as ReferCount from Order
let $a=(select sum(amount) as Amount, count(*) as Count from (traverse out('caused_order') from $parent.$current while out('includes').id contains 102) group by Amount)
where out('includes').id contains 102
select id as OrderID, $a[0].Amount as ReferSum, $a[0].Count as ReferCount from Order
let $a=(select sum(amount) as Amount, count(*) as Count from (traverse out('caused_order') from $parent.$current while out('includes').id contains 102) where $depth >= 1 group by Amount)
where out('includes').id contains 102