# Adding a field with a relationship to another field in my query

**URL:** <https://community.dynatrace.com/t/adding-a-field-with-a-relationship-to-another-field-in-my-query/42012>\
**Category:** DQL\
**Tags:** dql\
**Created:** [October 10, 2023, 3:49pm UTC](https://community.dynatrace.com/t/adding-a-field-with-a-relationship-to-another-field-in-my-query/42012 "2023-10-10T15:49:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_1](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@Christian\_1](https://community.dynatrace.com/u/Christian_1)\
**Post date:** [October 10, 2023, 3:49pm UTC](https://community.dynatrace.com/t/adding-a-field-with-a-relationship-to-another-field-in-my-query/42012/1 "2023-10-10T15:49:32Z")

</div>

I am trying to get a list of all the services affected by Davis problems and then add a field containing the host for each service. This is how I attempted to do it, but the fields that should contain host information are empty.

fetch events, from:now() - 4h  
| filter event.kind == “DAVIS\_PROBLEM”  
| expand affected\_entity\_ids  
| fields affected\_entity\_ids  
| lookup [fetch dt.entity.host], sourceField:affected\_entity\_ids, lookupField:runs[dt.entity.service]

If anyone can offer a solution, I would be very grateful

---

<div class="post-metadata">

**Author:** ![sinisa\_zubic](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/sinisa_zubic/32/32498_2.png) [@sinisa\_zubic](https://community.dynatrace.com/u/sinisa_zubic)\
**Post date:** [October 10, 2023, 4:18pm UTC](https://community.dynatrace.com/t/adding-a-field-with-a-relationship-to-another-field-in-my-query/42012/2 "2023-10-10T16:18:23Z")

</div>

Hi&nbsp;[@Christian\_](https://community.dynatrace.com/user/67605)

Following query should work for you

```markup
fetch events, from:now()-7d
| filter event.kind == "DAVIS_PROBLEM"
| summarize {problem=takeMax(record(timestamp,affected_entity_ids) )}, by:{display_id}
| expand affected_entity_id=problem[affected_entity_ids]
| fields display_id, affected_entity_id
| filter startsWith(affected_entity_id, "SERVICE-")
| lookup
  [
    fetch dt.entity.service
    | fieldsAdd hosts = runs_on[dt.entity.host]
  ], sourceField:affected_entity_id, lookupField:id, fields:{hosts, entity.name}
| expand dt.entity.host = hosts
| filter isNotNull(dt.entity.host)
| fields display_id, affected_service = affected_entity_id,service_name=entity.name, affected_host =dt.entity.host

```

You can also try it out in [Discover Dynatrace](http://fetch%20events,%20from:now()-7d%20%7C%20filter%20event.kind%20==%20%22DAVIS_PROBLEM%22%20%7C%20summarize%20%7Bproblem=takeMax(record(timestamp,affected_entity_ids)%20)%7D,%20by:%7Bdisplay_id%7D%20%7C%20expand%20affected_entity_id=problem%5Baffected_entity_ids%5D%20%7C%20fields%20display_id,%20affected_entity_id%20%7C%20filter%20startsWith(affected_entity_id,%20%22SERVICE-%22)%20%7C%20lookup%20%20%20%5B%20%20%20%20%20fetch%20dt.entity.service%20%20%20%20%20%7C%20fieldsAdd%20hosts%20=%20runs_on%5Bdt.entity.host%5D%20%20%20%5D,%20sourceField:affected_entity_id,%20lookupField:id,%20fields:%7Bhosts,%20entity.name%7D%20%7C%20expand%20dt.entity.host%20=%20hosts%20%7C%20filter%20isNotNull(dt.entity.host)%20%7C%20fields%20display_id,%20affected_service%20=%20affected_entity_id,service_name=entity.name,%20affected_host%20=dt.entity.host)

Best,  
Sini

---

<div class="post-metadata">

**Author:** ![Christian\_1](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@Christian\_1](https://community.dynatrace.com/u/Christian_1)\
**Post date:** [October 11, 2023, 10:00am UTC](https://community.dynatrace.com/t/adding-a-field-with-a-relationship-to-another-field-in-my-query/42012/3 "2023-10-11T10:00:13Z")

</div>

Yes, that worked perfectly, thank you!
