Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

- Dynatrace Community
- Dynatrace
- Ask
- DQL
- Re: Calculate a percentage?

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Pin this Topic for Current User
- Printer Friendly Page

Calculate a percentage?

Options

- Mark as New
- Subscribe to RSS Feed
- Permalink

14 May 2024 12:57 AM

Given two fields:

genCount = 2

vecEmbeddingCount = 2

I want to calculate the percentage of genCount of the total (ie. The result I'm looking for is 50%).

```
fetch logs
...
| fieldsAdd totalCount = genCount + vecEmbeddingCount
| fieldsAdd cacheHitRate = genCount / totalCount*100
```

What am I missing?

3 REPLIES 3

Options

- Mark as New
- Subscribe to RSS Feed
- Permalink

14 May 2024 06:43 AM

Order of operations matters here, especially these are integer operations:

2/4*100 = 0*100 = 0

but

100*2/4 = 200/4 = 50.

Here is DQL example:

```
data record(genCount = 2,vecEmbeddingCount = 2)
| fieldsAdd totalCount = genCount + vecEmbeddingCount
| fieldsAdd cacheHitRate = genCount / totalCount*100
| fieldsAdd cacheHitRateCorrect = 100*genCount / totalCount
```

Kris

Reply

Options

- Mark as New
- Subscribe to RSS Feed
- Permalink

14 May 2024 07:39 AM

Where is it specified that these are integers (or integer operations)?

In fact, integers aren't even mentioned in the DQL data types page - only double and long are listed.

This seems to work:

```
fetch logs
| summarize genCount = toDouble(countIf(matchesPhrase(content, "gpt-3.5"))),
vecEmbeddingCount = toDouble(countIf(matchesPhrase(content, "text-embedding-ada-002")))
| fieldsAdd totalCount = genCount + vecEmbeddingCount
| fieldsAdd cacheHitRate = genCount / totalCount * 100
| fieldsKeep cacheHitRate
```

Options

- Mark as New
- Subscribe to RSS Feed
- Permalink

14 May 2024 08:51 AM

I did not mean "integer data type", but meant "integer mathematical concept" (https://en.wikipedia.org/wiki/Integer)

When you convert "long" numbers to "double" numbers (floating point) it will work too, but will be give you result in form of "double". What type you want to get in the end of course depends on your requirements.

When you get result in form of floating point number, you can always get needed precision using ** round **(https://docs.dynatrace.com/docs/platform/grail/dynatrace-query-language/functions/mathematical-funct...) or needed data type i.e. using

Re: "order of operations" which often matter when dealing with integers . DQL uses most common approach uses by programming languages ("left to right"), probably that's why it was never mentioned in documentation. Example in second paragraph here covers similar case like ours: https://en.wikipedia.org/wiki/Order_of_operations#Programming_languages

Kris

An integer is the number zero ( 0), a positive natural number (1, 2, 3, etc.) or a negative integer ( −1, −2, −3, etc.).[1] The negative numbers are the additive inverses of the corresponding positive numbers.[2] The set of all integers is often denoted by the boldface Z or blackboard bold .[3] ...

In mathematics and computer programming, the order of operations is a collection of rules that reflect conventions about which operations to perform first in order to evaluate a given mathematical expression. These rules are formalized with a ranking of the operations. The rank of an operation is