Skip to main content

Advanced Giving Queries

Ready to unlock deeper insights from your giving data? These advanced queries will help you perform complex analysis, track trends, and generate sophisticated reports.

Query Customization Required

These example queries demonstrate common patterns but may require adjustments to match your specific database schema and field names. Test thoroughly in your environment before use.

Query Requirements

Schema Prefix

IMPORTANT: All tables in the Planning Center Giving module live in the planning_center schema. Always prefix table names with planning_center. in your queries. ✅ CORRECT: SELECT * FROM planning_center.giving_donations ❌ INCORRECT: SELECT * FROM giving_donations

Row Level Security (RLS)

Row Level Security automatically scopes results by:
  • tenant_organization_id – only data from your organization
  • system_status – active records returned by default
Do not add these filters manually—RLS already enforces them and redundant predicates can hide data or slow execution:
  • WHERE tenant_organization_id = 1
  • WHERE system_status = 'active'
Keep your attention on domain-specific filters (date ranges, refunded flags, fund logic) while RLS handles tenancy and system status.

Table of Contents

Complex Joins and Relationships

Complete Donation Details with All Relationships

This query brings together donations, donors, funds, batches, and campuses:

Multi-Fund Donations Analysis

Find donations that were split across multiple funds:

Time-Based Analysis

Year-Over-Year Comparison by Month

Donor Segmentation

Donor Lifecycle Analysis

Categorize donors by their giving patterns:

First-Time Donor Retention

Track how many first-time donors give again:

Recurring Giving Analysis

Active Recurring Donors

Recurring Giving Health Metrics

Pledge Campaign Tracking

Campaign Progress Dashboard

Individual Pledge Tracking

Window Functions and Rankings

Top Donors by Percentile

Performance Optimization

Using CTEs for Complex Queries

Efficient Date Range Queries

Best Practices for Advanced Queries

1. Use CTEs for Readability

Break complex queries into logical steps using Common Table Expressions (WITH clauses).

2. Optimize JOIN Order

Join smaller result sets first, then join to larger tables.

3. Use Appropriate Indexes

The relationship_type and relationship_id columns are indexed for efficient joins.

4. Aggregate Early

When possible, aggregate data in CTEs before joining to reduce the working set size.

5. Handle NULL Values

Always consider NULL values in calculations and use NULLIF to prevent division by zero.

Next Steps