# How to subtract arrays in DQL?

**URL:** <https://community.dynatrace.com/t/how-to-subtract-arrays-in-dql/50457>\
**Category:** DQL\
**Tags:** dql\
**Created:** [February 25, 2025, 4:45pm UTC](https://community.dynatrace.com/t/how-to-subtract-arrays-in-dql/50457 "2025-02-25T16:45:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bretonne](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@bretonne](https://community.dynatrace.com/u/bretonne)\
**Post date:** [February 25, 2025, 4:45pm UTC](https://community.dynatrace.com/t/how-to-subtract-arrays-in-dql/50457/1 "2025-02-25T16:45:58Z")

</div>

I want to find out the diff between two arrays and list them out with DQL.

Something like | fields array1, array2, diff

Diff needs to be calculated but not sure how to do that.

Here I have

array 1 = [“apple”, “orange”, “lychee”, “grapes”, “pineapple”]

array 2 = [“apple”, “orange”, “lychee”]

Then I would like diff result to be [“grapes”, “pineapple”].

How can I get diff through DQL?

thanks.

---

<div class="post-metadata">

**Author:** ![krzysztof\_hoja](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/krzysztof_hoja/32/34625_2.png) [@krzysztof\_hoja](https://community.dynatrace.com/u/krzysztof_hoja)\
**Post date:** [February 28, 2025, 8:27am UTC](https://community.dynatrace.com/t/how-to-subtract-arrays-in-dql/50457/2 "2025-02-28T08:27:59Z")

</div>

Yes, it is possible with iterative expressions and _ **arrayIndexOf** _ function. You need to check for each element of 1st array if it does not exist in 2nd array.

```markup
data record(a1=array( "apple", "orange", "lychee", "grapes", "pineapple" ), a2=array("apple", "orange", "lychee") )
| fieldsAdd diff = arrayRemoveNulls(iCollectArray(if ( arrayIndexOf( a2, a1[] ) < 0, a1[])))

```

 ![krzysztof_hoja_0-1740731256619.png](https://global.discourse-cdn.com/dynatrace/original/3X/8/f/8fbab545c855f53666261fff663d1b3fcd44bdf8.png)
