# Querying an instance based on the values of all its related instances properties

**URL:** <https://forum.typedb.com/t/querying-an-instance-based-on-the-values-of-all-its-related-instances-properties/202>\
**Category:** TypeQL\
**Created:** [12 September 2022 18:58 UTC](https://forum.typedb.com/t/querying-an-instance-based-on-the-values-of-all-its-related-instances-properties/202 "2022-09-12T18:58:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![timidak](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.typedb.com/timidak/32/82_2.png) [@timidak](https://forum.typedb.com/u/timidak)\
**Post date:** [12 September 2022 18:58 UTC](https://forum.typedb.com/t/querying-an-instance-based-on-the-values-of-all-its-related-instances-properties/202/1 "2022-09-12T18:58:38Z")

</div>

Hi there,  
I want to query an instance of a concept (concept-a) which has multiple relations to instances of concept-b.  
The query should only return the instance of concept-a if the all the properties “is-active” of the related instances are true.  
I’m not really getting it. Do you know how to implement the query?

```auto
    indication sub relation, relates condition, relates cause;

    concept-a sub entity,
        plays indication:cause,
        owns name;

    concept-b sub entity,
        plays indication:condition,
        owns name,
        owns is-active,

```

I can get all instances of concept-a where at least one related concept-b instance has an is-active state with true, but I want only those concept-a instances where every related concept-b instance has the is-active property set to true.

---

<div class="post-metadata">

**Author:** ![krishnan](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.typedb.com/krishnan/32/36_2.png) [@krishnan](https://forum.typedb.com/u/krishnan)\
**Post date:** [13 September 2022 14:07 UTC](https://forum.typedb.com/t/querying-an-instance-based-on-the-values-of-all-its-related-instances-properties/202/2 "2022-09-13T14:07:06Z")

</div>

You could try a nested negation.

```auto
match
$a isa concept-a; 
not{
   (cause: $a, condition: $b) isa indication;
   not { $b has is-active true; }; 
};

```

Note that this will also match `concept-a` that aren’t part of any indication, and those related to concept-b without any is-active attributes.

---

<div class="post-metadata">

**Author:** ![timidak](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.typedb.com/timidak/32/82_2.png) [@timidak](https://forum.typedb.com/u/timidak)\
**Post date:** [14 September 2022 20:10 UTC](https://forum.typedb.com/t/querying-an-instance-based-on-the-values-of-all-its-related-instances-properties/202/3 "2022-09-14T20:10:16Z")

</div>

Thank you @krishnan for your answer!  
I tried your solution and it doesn’t work for me.  
It basically gave me the same result as my solution which was this:

```nohighlight
match
    $a isa concept-a, has name $n;
    ($a $b) isa indication;
    $b has is-active true; 
    get $a, $n;

```

I thought about grouping and counting in the query. But I don’t know how to find all nodes connected to concept-a with a specific relation. Do you think that’s even possible?

---

<div class="post-metadata">

**Author:** ![joshua](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.typedb.com/joshua/32/28_2.png) [@joshua](https://forum.typedb.com/u/joshua)\
**Post date:** [15 September 2022 10:24 UTC](https://forum.typedb.com/t/querying-an-instance-based-on-the-values-of-all-its-related-instances-properties/202/4 "2022-09-15T10:24:04Z")

</div>

Krishnan’s query should be a bit different to yours:  
Yours conceptually does: find `a` such that there is any indication `b` that has `is-active = true`  
Krishnan’s query conceptually does: find a concept `a` that does not have any indication `b` which doesn’t have an `is-active = true`… in other words, find the `a`’s for which all `b`’s have `is-active = true` (or there are no `b`s [which also conforms to your requirement], or there are only b’s with no is-actives at all [which also conforms to your requirement])

Can you show some example data where Krishnan’s query does not work?

---

<div class="post-metadata">

**Author:** ![timidak](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.typedb.com/timidak/32/82_2.png) [@timidak](https://forum.typedb.com/u/timidak)\
**Post date:** [15 September 2022 20:01 UTC](https://forum.typedb.com/t/querying-an-instance-based-on-the-values-of-all-its-related-instances-properties/202/5 "2022-09-15T20:01:50Z")

</div>

```auto
insert $a isa concept-a, has name "Fault A"; 
insert $a isa concept-a, has name "Fault B";

insert $b isa concept-b, has name "Condition A", has is-active true;
insert $b isa concept-b, has name "Condition B", has is-active true;
insert $b isa concept-b, has name "Condition C", has is-active true;
insert $b isa concept-b, has name "Condition D", has is-active false;

match $a isa concept-a, has name "Fault A"; $b isa concept-b, has name "Condition A"; insert (condition: $b, cause: $a) isa indication;
match $a isa concept-a, has name "Fault A"; $b isa concept-b, has name "Condition B"; insert (condition: $b, cause: $a) isa indication;
match $a isa concept-a, has name "Fault B"; $b isa concept-b, has name "Condition C"; insert (condition: $b, cause: $a) isa indication;
match $a isa concept-a, has name "Fault B"; $b isa concept-b, has name "Condition D"; insert (condition: $b, cause: $a) isa indication;

```

Here is some example data.  
What I want to achieve is to get only the instance with the name Fault A, because both connected nodes have the property is active `true`. But with @krishnan’s query I also get the other one, that has one is-active true.

Edit: What I must say is that I don’t get any results without a is-active property set to true, what is pretty good, but I cannot figure out how to eliminate the concepts which don’t have alle connected conditions active.

---

<div class="post-metadata">

**Author:** ![krishnan](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.typedb.com/krishnan/32/36_2.png) [@krishnan](https://forum.typedb.com/u/krishnan)\
**Post date:** [20 September 2022 16:11 UTC](https://forum.typedb.com/t/querying-an-instance-based-on-the-values-of-all-its-related-instances-properties/202/6 "2022-09-20T16:11:00Z")

</div>

Strange.  
With this data, I only get `Fault A` for the query I posted.

---

<div class="post-metadata">

**Author:** ![timidak](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.typedb.com/timidak/32/82_2.png) [@timidak](https://forum.typedb.com/u/timidak)\
**Post date:** [20 September 2022 19:30 UTC](https://forum.typedb.com/t/querying-an-instance-based-on-the-values-of-all-its-related-instances-properties/202/7 "2022-09-20T19:30:37Z")

</div>

> [@krishnan](#):
>
> ```auto
> match
> $a isa concept-a; 
> not{
> (cause: $a, condition: $b) isa indication;
> not { $b has is-active true; }; 
> };
> 
> ```

I’m sorry @krishnan you’re totally right. Your solution works with the schema and data provided.  
I obfuscated the real schema and tried to apply your solution to my schema and it didn’t work.  
But it worked when I tried the example with the data here.  
Maybe I made a mistake translating the query to my “real” schema. I will try to fix that and maybe come back, if I don’t get it running. Thank you so much for helping me out!

---

<div class="post-metadata">

**Author:** ![timidak](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.typedb.com/timidak/32/82_2.png) [@timidak](https://forum.typedb.com/u/timidak)\
**Post date:** [21 September 2022 11:50 UTC](https://forum.typedb.com/t/querying-an-instance-based-on-the-values-of-all-its-related-instances-properties/202/8 "2022-09-21T11:50:09Z")

</div>

> [@krishnan](#):
>
> Note that this will also match `concept-a` that aren’t part of any indication, and those related to concept-b without any is-active attributes.

That was the error on my side, in my translated data I had a concept-b where without any `is-active` attribute. Thank you for the help.
