---
title: HKStatisticsCollectionQueryDescriptor
framework: healthkit
role: symbol
role_heading: Structure
path: healthkit/hkstatisticscollectionquerydescriptor
---

# HKStatisticsCollectionQueryDescriptor

A query descriptor that gathers a collection of statistics calculated over a series of fixed-length time intervals.

## Declaration

```swift
struct HKStatisticsCollectionQueryDescriptor
```

## Mentioned in

Reading data from HealthKit Running Queries with Swift Concurrency

## Overview

Overview Use HKStatisticsCollectionQueryDescriptor to run a query that calculates statistics grouped into time intervals. To get a snapshot of the current data in the store, create a descriptor and call its result(for:) method. // Create a predicate for this week's samples. let calendar = Calendar(identifier: .gregorian) let today = calendar.startOfDay(for: Date())

guard let endDate = calendar.date(byAdding: .day, value: 1, to: today) else {     fatalError("*** Unable to calculate the end time ***") }

guard let startDate = calendar.date(byAdding: .day, value: -7, to: endDate) else {     fatalError("*** Unable to calculate the start time ***") }

let thisWeek = HKQuery.predicateForSamples(withStart: startDate, end: endDate)

// Create the query descriptor. let stepType = HKQuantityType(.stepCount) let stepsThisWeek = HKSamplePredicate.quantitySample(type: stepType, predicate:thisWeek) let everyDay = DateComponents(day:1)

let sumOfStepsQuery = HKStatisticsCollectionQueryDescriptor(     predicate: stepsThisWeek,     options: .cumulativeSum,     anchorDate: endDate,     intervalComponents: everyDay)

let stepCounts = try await sumOfStepsQuery.result(for: store)

// Use the statistics collection here. To set up a long-running query that updates the calculations based on any new data that arrives while it’s running, call the results(for:) method instead. The first result contains calculations based on samples currently in the HealthKit store, and additional results represent updates as they occur. // Run a long-running query that updates its statistics as new data comes in. let updateQueue = sumOfStepsQuery.results(for: store)

// Wait for the initial results and updates. updateTask = Task {     for try await results in updateQueue {         // Use the statistics collection here.     } }

## Topics

### Creating Query Descriptors

- [init(predicate:options:anchorDate:intervalComponents:)](healthkit/hkstatisticscollectionquerydescriptor/init(predicate:options:anchordate:intervalcomponents:).md)

### Running Queries

- [result(for:)](healthkit/hkstatisticscollectionquerydescriptor/result(for:).md)
- [results(for:)](healthkit/hkstatisticscollectionquerydescriptor/results(for:).md)
- [HKStatisticsCollectionQueryDescriptor.Results](healthkit/hkstatisticscollectionquerydescriptor/results.md)

### Accessing Query Properties

- [predicate](healthkit/hkstatisticscollectionquerydescriptor/predicate.md)
- [options](healthkit/hkstatisticscollectionquerydescriptor/options.md)
- [anchorDate](healthkit/hkstatisticscollectionquerydescriptor/anchordate.md)
- [intervalComponents](healthkit/hkstatisticscollectionquerydescriptor/intervalcomponents.md)

### Structures

- [HKStatisticsCollectionQueryDescriptor.Result](healthkit/hkstatisticscollectionquerydescriptor/result.md)

### Default Implementations

- [HKAsyncQuery Implementations](healthkit/hkstatisticscollectionquerydescriptor/hkasyncquery-implementations.md)
- [HKAsyncSequenceQuery Implementations](healthkit/hkstatisticscollectionquerydescriptor/hkasyncsequencequery-implementations.md)

## Relationships

### Conforms To

- [Copyable](swift/copyable.md)
- [Escapable](swift/escapable.md)
- [HKAsyncQuery](healthkit/hkasyncquery.md)
- [HKAsyncSequenceQuery](healthkit/hkasyncsequencequery.md)

## See Also

### Statistics

- [Executing Statistical Queries](healthkit/executing-statistical-queries.md)
- [Executing Statistics Collection Queries](healthkit/executing-statistics-collection-queries.md)
- [HKStatisticsQueryDescriptor](healthkit/hkstatisticsquerydescriptor.md)
- [HKStatisticsQuery](healthkit/hkstatisticsquery.md)
- [HKStatisticsCollectionQuery](healthkit/hkstatisticscollectionquery.md)
- [HKStatistics](healthkit/hkstatistics.md)
- [HKStatisticsCollection](healthkit/hkstatisticscollection.md)
- [HKStatisticsOptions](healthkit/hkstatisticsoptions.md)
