AI_AGG

AI_AGG

Overview

Aggregates a text column and returns insights across multiple rows based on a user-defined prompt. Not subject to context window limitations per row.

Syntax

AI_AGG(
  text_column,
  instruction
)

Parameters

  • text_column (VARCHAR): Column containing text to aggregate
  • instruction (VARCHAR): Natural language prompt describing the desired aggregation

Use Cases

  • Summarize customer feedback across products
  • Extract common themes from reviews
  • Identify key insights from multiple records
  • Aggregate findings from grouped data
  • Generate executive summaries from detailed records

Code Examples

Example 1: Summarize Product Reviews

SELECT 
    product_id,
    AI_AGG(
        review_text,
        'Summarize the key themes in customer reviews'
    ) AS review_summary
FROM customer_reviews
GROUP BY product_id;

Output:

product_id | review_summary
-----------|---------------
101        | Customers praise battery life and performance but cite high price as concern
102        | Mixed feedback on durability; excellent display quality noted frequently

Example 2: Extract Common Complaints

SELECT 
    department,
    AI_AGG(
        ticket_description,
        'List the top 3 most common issues mentioned'
    ) AS common_issues
FROM support_tickets
WHERE created_date >= CURRENT_DATE - 30
GROUP BY department;

Example 3: Identify Opportunities

SELECT 
    region,
    AI_AGG(
        sales_notes,
        'Identify potential upsell opportunities mentioned by sales team'
    ) AS opportunities
FROM sales_calls
WHERE call_date >= CURRENT_DATE - 90
GROUP BY region;

Example 4: Generate Executive Summary

SELECT 
    AI_AGG(
        meeting_notes,
        'Create an executive summary highlighting decisions made and action items'
    ) AS executive_summary
FROM weekly_meetings
WHERE meeting_date >= DATE_TRUNC('month', CURRENT_DATE);

Example 5: Sentiment Analysis Across Groups

SELECT 
    product_category,
    AI_AGG(
        customer_feedback,
        'Summarize overall customer sentiment and provide specific examples of positive and negative feedback'
    ) AS sentiment_analysis
FROM feedback
GROUP BY product_category;

Data Output Examples

Customer Feedback Aggregation

Input: 20 customer reviews for a laptop
Instruction: "Summarize the main pros and cons"

Output:
"Pros: Excellent battery life (15+ mentions), fast performance, lightweight design.
Cons: High price point, limited port selection, loud fan noise under heavy load."

Support Ticket Analysis

Input: 50 support tickets
Instruction: "What are the recurring technical issues?"

Output:
"Top issues: 1) Login failures (40% of tickets), 2) Slow page load times (25%), 
3) Mobile app crashes (20%), 4) Payment processing errors (15%)"

Model Information

  • Model Used: Snowflake managed model
  • Context Window: 128,000 tokens per row (can aggregate across unlimited rows)
  • Max Output: 8,192 tokens

Limitations & Considerations

Input Processing

  • Each row can contain up to 128,000 tokens
  • Function processes all rows in the group
  • No hard limit on number of rows aggregated

Cost

  • Billing based on total input and output tokens
  • All aggregated text counts toward input tokens
  • Instruction counts as input for each group

Performance

  • Designed for batch processing
  • Use MEDIUM warehouse or smaller
  • Processing time scales with data volume

Regional Availability

  • AWS US West/East: ✓
  • Azure East US: ✓
  • EU regions: ✓
  • Cross-region inference: ✓

Best Practices

1. Use Detailed Instructions

-- Good: Specific task description
AI_AGG(
    review_text,
    'Summarize customer feedback for an investor report, highlighting product strengths, weaknesses, and recommended improvements'
)

-- Less effective: Vague instruction
AI_AGG(review_text, 'Summarize')

2. Pre-filter Data

-- Filter before aggregating for better relevance
SELECT 
    product_id,
    AI_AGG(
        review_text,
        'What are customers saying about product quality?'
    ) AS quality_feedback
FROM customer_reviews
WHERE rating <= 3  -- Focus on critical reviews
  AND created_date >= CURRENT_DATE - 90
GROUP BY product_id;

3. Appropriate Grouping

-- Group by meaningful dimensions
SELECT 
    product_category,
    price_range,
    AI_AGG(
        review_text,
        'Summarize value-for-money sentiment'
    ) AS value_perception
FROM reviews
GROUP BY product_category, price_range;

4. Combine with Metrics

SELECT 
    product_id,
    COUNT(*) AS review_count,
    AVG(rating) AS avg_rating,
    AI_AGG(
        review_text,
        'Extract the most frequently mentioned feature (positive or negative)'
    ) AS key_feature
FROM reviews
GROUP BY product_id
HAVING COUNT(*) >= 10;  -- Ensure sufficient data

Common Use Cases

Market Research

SELECT 
    competitor,
    AI_AGG(
        customer_comment,
        'What competitive advantages or disadvantages are mentioned?'
    ) AS competitive_analysis
FROM competitor_mentions
GROUP BY competitor;

Product Development

SELECT 
    feature_request_category,
    AI_AGG(
        user_request,
        'Prioritize feature requests by frequency and impact'
    ) AS prioritized_requests
FROM feature_requests
GROUP BY feature_request_category;

Related Functions

  • AI_SUMMARIZE_AGG - Specialized for summaries
  • AI_COMPLETE - For more complex analysis
  • AI_SENTIMENT - For sentiment-specific aggregation

Interested to Learn More? Try Out the Free 14-Days Trial

One AI-native workspace to build, ship, and monitor dbt™ — without the tool sprawl.