# How to write multiple fetch spans query

**URL:** <https://community.dynatrace.com/t/how-to-write-multiple-fetch-spans-query/54007>\
**Category:** DQL\
**Tags:** dql\
**Created:** [November 6, 2025, 9:17am UTC](https://community.dynatrace.com/t/how-to-write-multiple-fetch-spans-query/54007 "2025-11-06T09:17:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rishirajk](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@rishirajk](https://community.dynatrace.com/u/rishirajk)\
**Post date:** [November 6, 2025, 9:17am UTC](https://community.dynatrace.com/t/how-to-write-multiple-fetch-spans-query/54007/1 "2025-11-06T09:17:06Z")

</div>

Hi ,

I wish to know how to write multiple fetch spans queries plus i want to use them to perform basic arithmetic operations .  
I have tried using join statement however I am missing something .

fetch spans, scanLimitGBytes: -1  
| filter request.is\_root\_span == true AND isNotNull(endpoint.name)  
| filter (matchesValue(`k8s.workload.name`, “services”)  
OR matchesValue(`dt.kubernetes.workload.name`, “services”))  
| filter matchesValue(`endpoint.name`, “ID”)  
| summarize generated\_count = toLong(count())  
| fieldsAdd join\_key = 1  
| join [  
fetch spans, scanLimitGBytes: -1  
| filter request.is\_root\_span == true AND isNotNull(endpoint.name)  
| filter (matchesValue(`k8s.workload.name`, “services”)  
OR matchesValue(`dt.kubernetes.workload.name`, “services”))  
| filter matchesValue(`endpoint.name`, “Verify”)  
and (`http.response.status_code` == 202 OR toLong(http.status\_code) == 202  
OR `http.response.status_code` == 200 OR toLong(http.status\_code) == 200)  
| summarize verified\_count = toLong(count())  
| fieldsAdd join\_key = 1  
],  
on: { left[join\_key] == right[join\_key] },  
fields: { generated\_count, verified\_count }  
| join [  
fetch spans, scanLimitGBytes: -1  
| filter request.is\_root\_span == true AND isNotNull(endpoint.name)  
| filter (matchesValue(`k8s.workload.name`, “services”)  
OR matchesValue(`dt.kubernetes.workload.name`, “services”))  
| filter matchesValue(`endpoint.name`, “Verify”)  
and (`http.response.status_code` == 409 OR toLong(http.status\_code) == 409)  
| summarize failed\_count = toLong(count())  
| fieldsAdd join\_key = 1  
],  
on: { left[join\_key] == right[join\_key] },  
fields: { generated\_count, verified\_count, failed\_count }  
| fieldsAdd unused\_otp = generated\_count - (verified\_count + failed\_count)

---

<div class="post-metadata">

**Author:** ![t\_pawlak](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/t_pawlak/32/14991_2.png) [@t\_pawlak](https://community.dynatrace.com/u/t_pawlak)\
**Post date:** [November 7, 2025, 12:11pm UTC](https://community.dynatrace.com/t/how-to-write-multiple-fetch-spans-query/54007/2 "2025-11-07T12:11:01Z")

</div>

Hi! You don’t need multiple fetch + join queries for this. It’s much faster and simpler to do a single pass over spans and compute conditional counts directly.  
Try this:

```markup
fetch spans, scanLimitGBytes: -1
| filter request.is_root_span == true
| filter isNotNull(endpoint.name)
| filter matchesValue(k8s.workload.name, "services")
      OR matchesValue(dt.kubernetes.workload.name, "services")
| fieldsAdd status = toLong(coalesce(http.response.status_code, http.status_code))
| summarize
  generated_count = toLong(countIf(matchesValue(endpoint.name, "ID"))),
  verified_count = toLong(countIf(matchesValue(endpoint.name, "Verify") AND (status == 200 OR status == 202))),
  failed_count = toLong(countIf(matchesValue(endpoint.name, "Verify") AND status == 409))
| fieldsAdd unused_otp = generated_count - (verified_count + failed_count)

```

---

<div class="post-metadata">

**Author:** ![rishirajk](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@rishirajk](https://community.dynatrace.com/u/rishirajk)\
**Post date:** [November 7, 2025, 1:35pm UTC](https://community.dynatrace.com/t/how-to-write-multiple-fetch-spans-query/54007/3 "2025-11-07T13:35:46Z")

</div>

Thanks It works 🙂
