What you are describing is the expected behavior of the command you provided. Let's look at the stats command:
| stats avg(TotalTime) BY TotalTime
This doesn't really make sense as a command. Roughly translated to English, it says: "For each value of TotalTime, find all other events with the same value for TotalTime, and take the average of TotalTime." So if you have five events that each contain the TotalTime=12 , then Splunk will take all five events, sum up 12+12+12+12+12 and divide the total by the number of events (5) and return the average: 12 . And so on for every value of TotalTime that Splunk finds. So the command | stats avg(TotalTime) BY TotalTime will always yield two columns: avg(TotalTime) and TotalTime , and they will always have the same value. Follow that up with a table command that includes TotalTime but doesn't include avg(TotalTime) , and you'll only have values for TotalTime.
That brings us to the second part of the issue at hand with the stats call: if you don't specify a field in the stats call, it won't pass through that part of the query. So no matter how many fields you had before you called | stats avg(TotalTime) by TotalTime , you will only be left with two fields afterwards: TotalTime and avg(TotalTime) . If you remove the table command from the end of your search, you'll see that.
I'm not sure what quite what your intent is with the stats call, but I think you want the average of all TotalTime values as a column. If so, this might get you there:
index="jenkins-cicd-*" source="**/ctest-metrics-summary.json"
| rex max_match=0 field=_raw "(?<lineData>[^\n]+)"
| mvexpand lineData
| spath input=lineData path=env output=singleEnv
| spath input=singleEnv
| spath input=lineData
| eval status=mvindex(status,1)
| eval testRunStartTime=mvindex(testRunStartTime,1)
| eval testRunEndTime=mvindex(testRunEndTime,1)
| eval testFileName=mvindex(testFileName,1)
| eval testCaseName=mvindex(testCaseName,1)
| eval testCaseId=mvindex(testCaseId,1)
| eval TotalTime = strftime(strptime(testRunEndTime , "%Y-%m-%dT%H:%M:%S.%3N") - strptime(testRunStartTime, "%Y-%m-%dT%H:%M:%S.%3N"), "%Mm %Ss %2Nms")
| eventstats avg(TotalTime) AS AverageTime
| table status testRunStartTime testRunEndTime testFileName testCaseName testCaseId TotalTime AverageTime
... View more