> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# ds_theta_intersect_cond_agg

> Aggregate function that computes the intersection cardinality of two groups of Theta sketches in a single pass, routing rows by an anchor flag.

Aggregate function that maintains two independent theta-union sketches — an
**anchor** group (`is_anchor = 1`) and a **window** group (`is_anchor = 0`) —
then at finalization intersects the two unions and returns the cardinality
estimate of the intersection as `DOUBLE`.

This is a single-pass equivalent of:

```SQL theme={null}
ds_theta_estimate(
    ds_theta_intersect(
        ds_theta_combine(sketch) FILTER (WHERE is_anchor = 1),
        ds_theta_combine(sketch) FILTER (WHERE is_anchor = 0)
    )
)
```

## Syntax

```Haskell theme={null}
DOUBLE ds_theta_intersect_cond_agg(sketch, is_anchor)
```

* `sketch`: `VARBINARY` compact theta sketch, typically produced by
  [`ds_theta_accumulate`](/documents/4.2/sql-reference/sql-functions/aggregate-functions/ds_theta_accumulate) or
  [`ds_theta_combine`](/documents/4.2/sql-reference/sql-functions/aggregate-functions/ds_theta_combine).
* `is_anchor`: `INT` flag. Any nonzero value routes the sketch to the anchor
  group; `0` routes it to the window group.

## Return value

Returns `DOUBLE`. Returns `0` if either group receives no sketches.

## Examples

```SQL theme={null}
-- Distinct users who appeared in both the anchor cohort and the window cohort.
SELECT
    ds_theta_intersect_cond_agg(sketch, is_anchor) AS overlap_estimate
FROM (
    SELECT ds_theta_accumulate(user_id) AS sketch, 1 AS is_anchor
    FROM events WHERE cohort = 'anchor'
    UNION ALL
    SELECT ds_theta_accumulate(user_id) AS sketch, 0 AS is_anchor
    FROM events WHERE cohort = 'window'
) t;
```

## Keywords

DS\_THETA\_INTERSECT\_COND\_AGG, DS\_THETA\_COMBINE, DS\_THETA\_INTERSECT, DS\_THETA\_ESTIMATE


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.