<!--
{
  "availability" : [
    "iOS: 15.4.0 -",
    "iPadOS: 15.4.0 -",
    "macCatalyst: 15.4.0 -",
    "macOS: 13.0.0 -",
    "visionOS: -",
    "watchOS: 8.5.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "HealthKit",
  "identifier" : "/documentation/HealthKit/HKStatisticsQueryDescriptor",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "HealthKit"
    ],
    "preciseIdentifier" : "s:9HealthKit27HKStatisticsQueryDescriptorV"
  },
  "title" : "HKStatisticsQueryDescriptor"
}
-->

# HKStatisticsQueryDescriptor

A query descriptor that calculates the minimum, maximum, average, or sum over a set of samples from the HealthKit store.

```
struct HKStatisticsQueryDescriptor
```

## Overview

Use the [`HKStatisticsQueryDescriptor`](/documentation/HealthKit/HKStatisticsQueryDescriptor) to perform calculations over sets of samples currently saved in the HealthKit store.

```swift
// Create a predicate for today's samples.
let calendar = Calendar(identifier: .gregorian)
let startDate = calendar.startOfDay(for: Date())
let endDate = calendar.date(byAdding: .day, value: 1, to: startDate)
let today = HKQuery.predicateForSamples(withStart: startDate, end: endDate)

// Create the query descriptor.
let stepType = HKQuantityType(.stepCount)
let stepsToday = HKSamplePredicate.quantitySample(type: stepType, predicate:today)
let sumOfStepsQuery = HKStatisticsQueryDescriptor(predicate: stepsToday, options: .cumulativeSum)

// Run the query.
let stepCount = try await sumOfStepsQuery.result(for: store)?
    .sumQuantity()?
    .doubleValue(for: HKUnit.count())

// Use the step count here.
```

## Topics

### Creating Query Descriptors

[`init(predicate:options:)`](/documentation/HealthKit/HKStatisticsQueryDescriptor/init(predicate:options:))

Creates a statistics query descriptor.

### Running Queries

[`result(for:)`](/documentation/HealthKit/HKStatisticsQueryDescriptor/result(for:))

Runs a one-shot query and asynchronously returns a snapshot of the current matching results.

### Accessing Query Properties

[`predicate`](/documentation/HealthKit/HKStatisticsQueryDescriptor/predicate)

A predicate that defines the set of data that the query uses to calculate the statistics.

[`options`](/documentation/HealthKit/HKStatisticsQueryDescriptor/options)

A list of options that define the type of statistical calculations performed and the way in which HealthKit merges data from multiple sources.

## Relationships

### Conforms To

[`Escapable`](/documentation/Swift/Escapable)

[`HKAsyncQuery`](/documentation/HealthKit/HKAsyncQuery)

[`Copyable`](/documentation/Swift/Copyable)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)