Open History to see the last commit
1---
2id: GroupKeys
3title: GroupKeys
4---
5
6Defined in: [packages/db/src/query/builder/group-keys.ts:9](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/group-keys.ts#L9)
7
8Group keys for `query.groupBy(...)` when the query has no aggregate.
9Aggregates such as `count` bring the grouping compiler themselves.
10
11## Type Parameters
12
13### TContext
14
15`TContext` *extends* [`Context`](Context.md)
16
17## Properties
18
19### callback
20
21```ts
22callback: GroupByCallback<TContext>;
23```
24
25Defined in: [packages/db/src/query/builder/group-keys.ts:10](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/group-keys.ts#L10)
26
27***
28
29### grouping
30
31```ts
32grouping: object;
33```
34
35Defined in: [packages/db/src/query/builder/group-keys.ts:11](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/group-keys.ts#L11)
36
37#### processGroupBy()
38
39```ts
40processGroupBy: (pipeline, groupByClause, valueIdentity, isSourceRef, havingClauses?, selectClause?, fnHavingClauses?, aggregateCollectionId?, mainSource?, sanitizeCallbackRows) => NamespacedAndKeyedStream;
41```
42
43Processes the GROUP BY clause with optional HAVING and SELECT
44Works with the new $selected structure from early SELECT processing
45
46##### Parameters
47
48###### pipeline
49
50[`NamespacedAndKeyedStream`](../type-aliases/NamespacedAndKeyedStream.md)
51
52###### groupByClause
53
54[`GroupBy`](../@tanstack/namespaces/IR/type-aliases/GroupBy.md)
55
56###### valueIdentity
57
58`ValueIdentity`
59
60###### isSourceRef
61
62(`ref`) => `boolean`
63
64Whether a ref reads a field of the query's own rows.
65
66###### havingClauses?
67
68[`Where`](../@tanstack/namespaces/IR/type-aliases/Where.md)[]
69
70###### selectClause?
71
72[`Select`](../@tanstack/namespaces/IR/type-aliases/Select.md)
73
74###### fnHavingClauses?
75
76(`row`) => `any`[]
77
78###### aggregateCollectionId?
79
80`string`
81
82###### mainSource?
83
84`string`
85
86###### sanitizeCallbackRows?
87
88`boolean` = `false`
89
90##### Returns
91
92[`NamespacedAndKeyedStream`](../type-aliases/NamespacedAndKeyedStream.md)
93
94#### replaceAggregatesByRefs()
95
96```ts
97replaceAggregatesByRefs: (havingExpr, selectClause, resultAlias) => BasicExpression;
98```
99
100Transforms expressions to replace aggregate functions with references to computed values.
101
102For aggregate expressions, finds matching aggregates in the SELECT clause and replaces them
103with PropRef([resultAlias, alias]) to reference the computed aggregate value.
104
105Ref expressions (table columns and $selected fields) and value expressions are passed through unchanged.
106Function expressions are recursively transformed.
107
108##### Parameters
109
110###### havingExpr
111
112The expression to transform (can be aggregate, ref, func, or val)
113
114[`Aggregate`](../@tanstack/namespaces/IR/classes/Aggregate.md)\<`any`\> | [`BasicExpression`](../@tanstack/namespaces/IR/type-aliases/BasicExpression.md)
115
116###### selectClause
117
118[`Select`](../@tanstack/namespaces/IR/type-aliases/Select.md)
119
120The SELECT clause containing aliases and aggregate definitions
121
122###### resultAlias
123
124`string` = `...`
125
126The namespace alias for SELECT results (default: '$selected')
127
128##### Returns
129
130[`BasicExpression`](../@tanstack/namespaces/IR/type-aliases/BasicExpression.md)
131
132A transformed BasicExpression that references computed values instead of raw expressions