# Using a first query on second query data

**URL:** <https://community.dynatrace.com/t/using-a-first-query-on-second-query-data/54680>\
**Category:** DQL\
**Tags:** dql, dynatrace-managed\
**Created:** [January 19, 2026, 9:27pm UTC](https://community.dynatrace.com/t/using-a-first-query-on-second-query-data/54680 "2026-01-19T21:27:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ellery](https://avatars.discourse-cdn.com/v4/letter/e/5e9695/32.png) [@Ellery](https://community.dynatrace.com/u/Ellery)\
**Post date:** [January 19, 2026, 9:27pm UTC](https://community.dynatrace.com/t/using-a-first-query-on-second-query-data/54680/1 "2026-01-19T21:27:54Z")

</div>

Hello people.

i have big issue, im playing with spans, and y need obtains a metric.

my query

```markup
fetch spans
| filter isNotNull( http.request.header.host)
//| fields endpoint.name, http.request.header.host, http.request.method, trace.id
and endpoint.name == "/api/inicio/v1/empresa/estadisticasV2" 
| fieldsAdd http.response.status_code, response_time = end_time - start_time
| summarize percentile(response_time, 90), promedio = avg(response_time), maximo = max(response_time), count(), by:{endpoint.name}

| append 
  [ fetch spans 
   | filter isNotNull( http.request.header.host) and endpoint.name == "/api/inicio/v1/empresa/estadisticasV2" 
   | fieldsAdd http.response.status_code, response_time = end_time - start_time
   | summarize countIf(response_time >= promedio), countIf(response_time >= maximo) ,by:{endpoint.name} 
  ]

```

i want get the record count over promedio and count the max.

but my result was

 ![Ellery_0-1768850744722.png](https://global.discourse-cdn.com/dynatrace/original/3X/5/0/503c1dfbbf0310f883a31fa31f881a2bfd089870.png)

thanks for your help !!

---

<div class="post-metadata">

**Author:** ![peter\_zahrer](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/peter_zahrer/32/38745_2.png) [@peter\_zahrer](https://community.dynatrace.com/u/peter_zahrer)\
**Post date:** [January 20, 2026, 8:11am UTC](https://community.dynatrace.com/t/using-a-first-query-on-second-query-data/54680/2 "2026-01-20T08:11:15Z")

</div>

Hi,

first, i hope i understand your requirement / intended result correctly. A couple of observations upfront:

- you do not need to calculate the response time of a span manually, it is done by Dynatrace and available via the **duration** field, see example.
- Your query can never show a number of spans with a response time larger than the maximum as you are using the same timeframe for both queries. So the only possibility is that there are spans with a duration equal to the max, but never larger as the max was already found by the first query. Please clarify and provide more detail on your use case.

Here is my interpretation & example:

There is a certain reference of “performance” for certain requests. This reference can be defined by e.g. the observed response time during a certain timeframe (last business week in my case, or e.g. yesterday). Then, the current performance is compared to that reference and you want to see if your services are performing better or worse than before.

Thus i compiled this example:

```markup
fetch spans, from:@w1 //this business week, from Monday to now
| fields start_time, end_time, duration, endpoint.name
| filter endpoint.name == "/api/products"
| join [

        fetch spans, from:-1w@w1, to:@w5 //last business week
        | fields start_time, end_time, duration, endpoint.name
        | filter endpoint.name == "/api/products"
        | summarize promedio=avg(duration), maximo=max(duration), by:endpoint.name

], on:endpoint.name, prefix:"reference."
| summarize mas_que_promedio = countIf(duration >= reference.promedio), mas_que_maximo = countIf(duration >= reference.maximo)

```

you can copy paste this example and try it on our [Playground environment](https://wkf10640.apps.dynatrace.com/ui/apps/dynatrace.notebooks) based on real data.

Ultimately in case you need to be alerted in case the response time of your requests exceeds your specified thresholds consider using [Dynatrace anomaly detection](https://docs.dynatrace.com/docs/discover-dynatrace/platform/davis-ai/anomaly-detection/set-up-a-customized-anomaly-detector/how-to-set-up/anomaly-detection-app).

---

<div class="post-metadata">

**Author:** ![Ellery](https://avatars.discourse-cdn.com/v4/letter/e/5e9695/32.png) [@Ellery](https://community.dynatrace.com/u/Ellery)\
**Post date:** [January 20, 2026, 12:08pm UTC](https://community.dynatrace.com/t/using-a-first-query-on-second-query-data/54680/3 "2026-01-20T12:08:42Z")

</div>

Work’s Thank’s a lot !!!😁
