我已经搜索过此文件,但运气不佳。我在OrientDb(3.0.2)中建立了图形数据库,并且子查询遇到了一些问题。我将能够在经典的关系数据库中轻松编写此代码。我曾与LET和$ parent / $ children玩过,但这些概念仍然有些令人困惑。
三个顶点:实体<-检查->违规
我想要每个实体的最后一次检查(日期),并想知道与该检查相关的违规次数。
我知道如何为每次检查(边的size())提取违规数量。我也知道如何延长每个实体的最长检验日期,但是将这两个实体结合起来一直很困难。有什么想法吗?谢谢!
更新:我想我越来越近了,但仍然不确定如何进行“内部”加入。不知道我是否完全理解$ parent / $ current变量的工作方式。 此查询无效
SELECT
entityId
,$a.num_violations
,max(Date) as mDate
FROM (
SELECT
@rid AS entityId
,in('COMPLETED_AT').Date as Date
FROM Entity
unwind Date
)
let $a = (
SELECT
Date
,out('FOUND_DURING').size() as num_violations
FROM Inspection
WHERE $parent.mDate = Date
AND $parent.entityId in out('COMPLETED_AT').@rid
)
GROUP BY entityId
,$a.num_violations
答案 0 :(得分:1)
尝试一下:
select expand($c)
let $a = (select date, out("link")[@class="Violations"].size() as num_violations from Inspection),
$b = (select name, max(date) as max_date from (select name, in("link").date as date from Entity unwind date) group by name),
$c = unionAll($a, $b)
这就是你得到的:
+----+-------------------+--------------+----------+-------------------+
|# |date |num_violations|name |max_date |
+----+-------------------+--------------+----------+-------------------+
|0 |2018-08-31 00:00:00|1 | | |
|1 |2018-08-30 00:00:00|2 | | |
|2 |2018-08-29 00:00:00|1 | | |
|3 | | |entity_001|2018-08-31 00:00:00|
|4 | | |entity_002|2018-08-29 00:00:00|
+----+-------------------+--------------+----------+-------------------+
更新
select expand($c)
let $a = (select name as entity_name, max(date) as max_date from (select name, in("link").date as date from Entity unwind date) group by name),
$b = (select out("link")[@class="Violations"].size() as num_violations from Inspection),
$c = unionAll($a, $b)
+----+-----------+-------------------+--------------+
|# |entity_name|max_date |num_violations|
+----+-----------+-------------------+--------------+
|0 |entity_001 |2018-08-31 00:00:00| |
|1 |entity_002 |2018-08-29 00:00:00| |
|2 | | |1 |
|3 | | |2 |
|4 | | |1 |
+----+-----------+-------------------+--------------+
希望有帮助
致谢