我的数据库中有以下课程:
然后我必须从具有特定日期的订单中选择所有商品,对其进行分组并进行一些汇总,例如每种商品的数量总和。在stadard SQL DB中我会做几个连接,按日期和按goods_id分组,但无法弄清楚如何在OrientDB中实现这一点。我是从
开始的select expand(spec) from Order where date = "..."
但是当我添加分组时:
select expand(spec) from Order group by spec.goods
结果为空。
一般来说,我想找到以下SQL查询的等价物:
select goods_id, goods_name, sum(order_spec_quantity), sum(goods_price) from Order
left join OrderSpec on order_id = order_spec_order_id
left join Goods on goods_id = order_spec_goods_id
where date = 'some date'
group by goods_id, goods_name
答案 0 :(得分:0)
使用OrientDB,您可以选择图形数据库,直接使用“顶点”和“边”从数据库中删除PK和FK字段。 我创建了一个包含代码和查询的示例:
create class Goods extends V;
create property Goods.id integer;
create property Goods.name String;
create property Goods.price double;
create property Goods.description String;
create class Order extends V;
create property Order.id integer;
create property Order.dateorder date;
create property Order.country String;
create class orderSpec extends E;
create property orderSpec.quantity integer;
insert into Goods(id,name,price,description) values (1,"oven",260.5,"kitchen");
insert into Goods(id,name,price,description) values (2,"television",300.7,"sitting room");
insert into Goods(id,name,price,description) values (3,"bed",100.3,"bedroom");
insert into Goods(id,name,price,description) values (4,"table",120.4,"dining room");
insert into Order(id,dateorder,country) values (1,"2013-04-12","UK");
insert into Order(id,dateorder,country) values (2,"2014-07-12","Germany");
insert into Order(id,dateorder,country) values (3,"2014-07-12","UK");
create edge orderSpec from (select from Order where id=1) to (select from Goods where id=1) set quantity = 2;
create edge orderSpec from (select from Order where id=1) to (select from Goods where id=2) set quantity = 3;
create edge orderSpec from (select from Order where id=1) to (select from Goods where id=3) set quantity = 2;
create edge orderSpec from (select from Order where id=2) to (select from Goods where id=2) set quantity = 1;
create edge orderSpec from (select from Order where id=3) to (select from Goods where id=2) set quantity = 1;
create edge orderSpec from (select from Order where id=3) to (select from Goods where id=4) set quantity = 2;
这就是图表:
现在,您可以使用顶点之间的直接链接查询数据库以获取所有没有连接操作的信息。
按文章过滤:
按日期过滤:
答案 1 :(得分:0)
尝试以下查询
select myRid,myName,sum,eval("myPrice * sum") as totalPrice from (select myRid,myName,sum(myQuantity) as sum,myPrice from (select spec.goods.@rid as myRid,spec.goods.name as myName,spec.quantity as myQuantity, spec.goods.price as myPrice from (select spec from Order where date = 'some date' unwind spec )) group by myRid,myName)