From f9c5a00f8a07f0efd117cb9d76c666a98eab97b0 Mon Sep 17 00:00:00 2001 From: Rene Cannao Date: Mon, 19 Jan 2026 07:18:47 +0000 Subject: [PATCH] chore: Delete temporary discovery output files Remove temporary discovery output files that were previously generated but are now managed elsewhere. --- .../tmp/global_database_summary.md | 534 ------ .../tmp/question_templates.md | 1474 ----------------- 2 files changed, 2008 deletions(-) delete mode 100644 scripts/mcp/DiscoveryAgent/ClaudeCode_Headless/tmp/global_database_summary.md delete mode 100644 scripts/mcp/DiscoveryAgent/ClaudeCode_Headless/tmp/question_templates.md diff --git a/scripts/mcp/DiscoveryAgent/ClaudeCode_Headless/tmp/global_database_summary.md b/scripts/mcp/DiscoveryAgent/ClaudeCode_Headless/tmp/global_database_summary.md deleted file mode 100644 index 8c370296c..000000000 --- a/scripts/mcp/DiscoveryAgent/ClaudeCode_Headless/tmp/global_database_summary.md +++ /dev/null @@ -1,534 +0,0 @@ -# Global Database Summary - Codebase Community Template -## Comprehensive Discovery Report - ---- - -## Executive Summary - -The **Codebase Community Template** database is a Stack Overflow-style community Q&A platform containing **8 tables** with approximately **885,000 total records**. This database models a complete question-and-answer ecosystem with user reputation systems, content moderation, voting mechanics, badges/achievements, and comprehensive activity tracking. - -### Key Statistics -- **Total Records**: ~885,000 rows across all tables -- **Total Tables**: 8 core tables -- **Foreign Key Relationships**: 14 documented relationships -- **Time Span**: Community activity from 2010 to present -- **Core Entities**: Users, Posts, Comments, Votes, Badges, Tags, History, Links - ---- - -## Database Purpose and Scope - -This database is designed to track and manage a **technical Q&A community** where: -- Users can ask questions and provide answers -- Community voting determines content quality -- Reputation system rewards valuable contributions -- Tags organize content by topic -- Badges recognize user achievements -- Complete edit history maintains content integrity - ---- - -## Core Entities and Relationships - -### 1. **users** (40,325 records) -**Purpose**: Central user entity storing authentication, reputation, and profile data - -**Key Attributes**: -- `Id`: Primary key (User ID -1 is the system/community account) -- `Reputation`: User's reputation score (accumulated through upvotes) -- `CreationDate`: When the user account was created -- `DisplayName`: Public display name -- `Location`: Geographic location -- `Views`: Profile view count -- `UpVotes`/`DownVotes`: Total votes the user has cast -- `AccountId`: Network account ID (for multi-site login) - -**Business Rules**: -- Reputation is calculated from upvotes on user's posts -- Users can vote (upvote/downvote) on content -- Profile views indicate user visibility -- Age and website URL are optional demographic data - ---- - -### 2. **posts** (91,960 records) -**Purpose**: Core content table holding both questions and answers - -**Key Attributes**: -- `Id`: Primary key -- `PostTypeId`: Discriminator (1 = Question, 2 = Answer) -- `ParentId`: For answers, points to the question (self-referencing FK) -- `OwnerUserId`: Author of the post -- `Title`: Question title (only for PostTypeId = 1) -- `Body`: Content (HTML/Markdown) -- `Tags`: Tag list (format: ``) -- `Score`: Net vote score (upvotes - downvotes) -- `ViewCount`: Number of views (questions only) -- `AnswerCount`: Number of answers (questions only) -- `AcceptedAnswerId`: ID of the accepted answer (questions only) -- `CommentCount`: Number of comments -- `FavoriteCount`: Times favorited by users -- `CreationDate`: When post was created -- `LastActivityDate`: Last edit or comment -- `ClosedDate`: If/when question was closed -- `CommunityOwnedDate`: If post became community wiki - -**Business Rules**: -- Questions have Title, Tags, AnswerCount, ViewCount -- Answers have ParentId pointing to question -- Posts can be edited (tracked in postHistory) -- Questions can have one accepted answer -- Posts can become community wikis (no reputation earned) -- Posts can be closed by moderators - -**Critical Note**: Column name typo detected: `CreaionDate` should be `CreationDate` - ---- - -### 3. **comments** (174,218 records) -**Purpose**: Discussion and clarification on posts - -**Key Attributes**: -- `Id`: Primary key -- `PostId`: Foreign key to posts -- `UserId`: Comment author (nullable for anonymous) -- `Text`: Comment content -- `Score`: Net votes on comment -- `CreationDate`: When comment was posted -- `UserDisplayName`: Display name for anonymous comments - -**Business Rules**: -- Comments can be voted on (score) -- Users can delete comments (soft delete) -- Anonymous comments allowed (UserId NULL) - ---- - -### 4. **votes** (38,930 records) -**Purpose**: Records all voting activity on posts - -**Key Attributes**: -- `Id`: Primary key -- `PostId`: Post being voted on -- `VoteTypeId`: Type of vote (2 = UpVote, 3 = DownVote, etc.) -- `UserId`: Voter (nullable for anonymous/system votes) -- `CreationDate`: When vote was cast -- `BountyAmount`: If bounty was awarded - -**Business Rules**: -- Users can upvote or downvote posts -- Vote affects post's Score -- User cannot vote on their own posts -- Anonymous votes possible (system/voter privacy) - ---- - -### 5. **badges** (79,851 records) -**Purpose**: Achievement and gamification system - -**Key Attributes**: -- `Id`: Primary key -- `UserId`: Badge recipient -- `Name`: Badge name (e.g., "Teacher", "Student", "Enlightened") -- `Date`: When badge was earned - -**Business Rules**: -- Badges are awarded for various achievements -- Multiple users can earn the same badge -- Users can earn the same badge multiple times (some badge types) - ---- - -### 6. **tags** (1,031 records) -**Purpose**: Taxonomy system for organizing content - -**Key Attributes**: -- `Id`: Primary key -- `TagName`: Tag name (unique) -- `Count`: Number of questions with this tag -- `ExcerptPostId`: Post ID for tag wiki excerpt -- `WikiPostId`: Post ID for full tag wiki - -**Business Rules**: -- Tags categorize questions by topic -- Tag count reflects popularity -- Tags have wiki pages for detailed descriptions -- Tags can be synonyms (redirects) - ---- - -### 7. **postHistory** (303,100 records) -**Purpose**: Complete audit trail of all post edits - -**Key Attributes**: -- `Id`: Primary key -- `PostId`: Post that was edited -- `PostHistoryTypeId`: Type of edit (title, body, tags, etc.) -- `UserId`: Editor (nullable for system edits) -- `CreationDate`: When edit was made -- `Text`: New content -- `Comment`: Edit reason/comment -- `RevisionGUID`: Unique identifier for revision group -- `UserDisplayName`: Display name for anonymous edits - -**Business Rules**: -- Every edit creates a history record -- Multiple edits can be grouped in one revision -- Text field contains the new value -- Original title/body stored in initial revision - ---- - -### 8. **postLinks** (11,098 records) -**Purpose**: Relationships between posts (duplicates, related) - -**Key Attributes**: -- `Id`: Primary key -- `PostId`: Source post -- `RelatedPostId`: Target post (linked post) -- `LinkTypeId`: Type of link (1 = duplicate, 3 = related) -- `CreationDate`: When link was created - -**Business Rules**: -- Questions can be marked as duplicates -- Users can link related questions -- Links are directional (PostId → RelatedPostId) - ---- - -## Relationship Map - -### Primary Foreign Key Connections - -``` -users (1) ────────── (N) posts - │ │ - │ │ (self-ref) - │ │ - ├───────── (N) comments │ - │ │ - ├───────── (N) votes │ - │ │ - └───────── (N) badges │ - │ -posts (1) ──── (N) comments -posts (1) ──── (N) votes -posts (1) ──── (N) postHistory -posts (1) ──── (N) postLinks (PostId) -posts (1) ──── (N) postLinks (RelatedPostId) -posts (N) ──── (1) tags (via Tags text field) -``` - -### Join Patterns - -**1. User with their posts**: -```sql -users JOIN posts ON users.Id = posts.OwnerUserId -``` - -**2. Question with its answers**: -```sql -questions (PostTypeId=1) LEFT JOIN answers (PostTypeId=2) - ON questions.Id = answers.ParentId -``` - -**3. Post with comments and user info**: -```sql -posts - JOIN comments ON posts.Id = comments.PostId - JOIN users ON comments.UserId = users.Id -``` - -**4. Post with votes**: -```sql -posts JOIN votes ON posts.Id = votes.PostId -``` - -**5. User's badges**: -```sql -users JOIN badges ON users.Id = badges.UserId -``` - -**6. Complete post history**: -```sql -posts JOIN postHistory ON posts.Id = postHistory.PostId -``` - -**7. Linked/related posts**: -```sql -posts AS p1 - JOIN postLinks ON p1.Id = postLinks.PostId - JOIN posts AS p2 ON postLinks.RelatedPostId = p2.Id -``` - ---- - -## Domain Model (5 Domains) - -### Domain 1: **User Management** -**Tables**: `users` -**Purpose**: User accounts, authentication, profiles -**Key Metrics**: Reputation, profile views, account age, location -**Business Questions**: -- Who are our top contributors? -- What is the user retention rate? -- How does reputation distribute across users? - -### Domain 2: **Content Management** -**Tables**: `posts`, `postHistory` -**Purpose**: Q&A content, revisions, quality tracking -**Key Metrics**: Post count, answer rate, acceptance rate, edit frequency -**Business Questions**: -- What percentage of questions get answered? -- How quickly are questions answered? -- Which posts are most viewed? - -### Domain 3: **Engagement & Interaction** -**Tables**: `votes`, `comments` -**Purpose**: Community participation, voting, discussions -**Key Metrics**: Vote count, comment rate, engagement score -**Business Questions**: -- How active is the community? -- What is the upvote/downvote ratio? -- Which posts generate most discussion? - -### Domain 4: **Recognition & Gamification** -**Tables**: `badges` -**Purpose**: User achievements, incentives -**Key Metrics**: Badges earned, badge types, achievement rate -**Business Questions**: -- What badges are most common? -- Who are the top badge earners? -- How do badges correlate with activity? - -### Domain 5: **Content Organization** -**Tables**: `tags`, `postLinks` -**Purpose**: Taxonomy, categorization, duplicate detection -**Key Metrics**: Tag usage, expert identification, duplicate rate -**Business Questions**: -- What are the most popular tags? -- Which tags have most unanswered questions? -- Who are the experts for each tag? - ---- - -## Key Metrics and KPIs (25 Defined) - -### User Engagement (5 metrics) -1. **Active Users** - Users with posts in last 30 days -2. **Reputation Distribution** - Percentiles (25th, 50th, 75th, 90th, 99th) -3. **User Retention Rate** - % users with multiple posts -4. **Top Contributors** - Top 10 by reputation -5. **Voting Activity** - Upvote/downvote ratio - -### Content Quality (5 metrics) -6. **Question Answer Rate** - % questions with answers -7. **Answer Acceptance Rate** - % answered questions with accepted answer -8. **Average Response Time** - Hours to first answer (median, p75, p90) -9. **Question Closure Rate** - % questions closed -10. **Community Wiki Rate** - % posts becoming community wikis - -### Platform Health (5 metrics) -11. **Daily Question Volume** - New questions per day -12. **Comment Rate** - Average comments per post -13. **Vote Velocity** - Votes per post per day -14. **Edit Activity** - Post edits per day -15. **Badge Acquisition** - Badges earned per day - -### Tag Analytics (5 metrics) -16. **Top Tags** - Most frequently used tags -17. **Tag Specialization** - Questions and users per tag -18. **Unanswered by Tag** - Tags with highest unanswered rate -19. **Expertise by Tag** - Top users for each tag -20. **Trending Tags** - Fastest growing tags - -### Content Analytics (5 metrics) -21. **Most Viewed** - Top questions by views -22. **Fastest Answered** - Questions answered most quickly -23. **Most Controversial** - Posts with high up/down vote split -24. **Most Discussed** - Posts with most comments -25. **Answer Quality** - Accepted vs non-accepted answer scores - ---- - -## Natural Language Capabilities - -This database can answer **40+ question templates** across 4 categories: - -### User Analytics (10 questions) -- "Who are the top users by reputation?" -- "What is the activity summary for user X?" -- "How many users joined each month?" -- "Who are the most active users?" -- "What is the answer acceptance rate for users?" - -### Content Analytics (10 questions) -- "What are the most viewed questions about Python?" -- "What questions have no answers?" -- "What are the highest scored posts?" -- "How do accepted answers compare to non-accepted?" -- "What is the edit history for post X?" - -### Engagement Analytics (10 questions) -- "What posts have the most comments?" -- "Who are the most active commenters?" -- "What is the voting trend?" -- "What is the vote distribution for post X?" -- "Who are the most active voters?" - -### Tag Analytics (10 questions) -- "What are the most popular tags?" -- "What questions have both Python and Pandas tags?" -- "Who are the top experts for R?" -- "What tags have the highest unanswered rate?" -- "What tags are commonly used together?" - ---- - -## Data Quality Insights - -### Strengths -1. **Comprehensive audit trail**: Every edit tracked in postHistory -2. **Rich metadata**: Creation dates, scores, view counts on most entities -3. **Self-documenting**: Tag wikis, post comments explain content -4. **Scalable design**: Normalized structure supports millions of records - -### Known Issues -1. **Column typo**: `CreaionDate` instead of `CreationDate` in posts table -2. **Nullable FKs**: Some OwnerUserIds can be NULL (anonymous posts) -3. **Denormalized tags**: Tags stored as text string, not lookup table -4. **Soft deletes**: Comments/posts may be deleted but not removed from tables - -### Data Patterns -- **User ID -1**: System/community account -- **PostTypeId 1**: Questions -- **PostTypeId 2**: Answers -- **VoteTypeId 2**: UpVotes -- **VoteTypeId 3**: DownVotes -- **Tag format**: `` in XML-like syntax - ---- - -## Typical Use Cases - -### 1. Community Health Monitoring -```sql --- Daily active users, questions, answers -SELECT DATE(CreaionDate), COUNT(DISTINCT OwnerUserId) -FROM posts -GROUP BY DATE(CreaionDate); -``` - -### 2. Expert Identification -```sql --- Top answerers by tag -SELECT u.DisplayName, COUNT(*) as answer_count -FROM posts a -JOIN posts q ON a.ParentId = q.Id -JOIN users u ON a.OwnerUserId = u.Id -WHERE q.Tags LIKE '%%' -GROUP BY u.DisplayName -ORDER BY answer_count DESC; -``` - -### 3. Content Quality Analysis -```sql --- Answer rate by tag -SELECT - SUBSTRING_INDEX(SUBSTRING_INDEX(Tags, '><', n.n), '>', -1) as tag, - AVG(AnswerCount) as avg_answers, - SUM(CASE WHEN AnswerCount = 0 THEN 1 ELSE 0 END) * 100.0 / COUNT(*) as unanswered_pct -FROM posts -CROSS JOIN (SELECT 1 as n UNION ALL SELECT 2 ...) nums -WHERE PostTypeId = 1 -GROUP BY tag; -``` - -### 4. User Reputation Analytics -```sql --- Reputation distribution -SELECT - NTILE(10) OVER (ORDER BY Reputation) as decile, - MIN(Reputation) as min_rep, - MAX(Reputation) as max_rep, - COUNT(*) as user_count -FROM users -GROUP BY NTILE(10) OVER (ORDER BY Reputation); -``` - ---- - -## Technical Recommendations - -### For Analytics -1. **Create indexes** on: CreationDate, OwnerUserId, PostTypeId, Score -2. **Materialize tag relationships** for faster tag-based queries -3. **Partition posts** by CreationDate for time-series analysis -4. **Create summary tables** for daily/monthly metrics - -### For Application Development -1. **Fix column typo**: Rename `CreaionDate` to `CreationDate` -2. **Add composite indexes**: (PostTypeId, CreationDate), (OwnerUserId, Score) -3. **Consider caching**: User reputation, tag counts (updated periodically) -4. **Implement soft deletes**: Track deleted posts with is_deleted flag - -### For Data Science -1. **Feature engineering**: - - User activity rate (posts/day) - - Answer quality score - - Tag expertise score - - Engagement velocity -2. **Predictive modeling**: - - Question likelihood of being answered - - User churn prediction - - Answer acceptance prediction - - Trending tag prediction - ---- - -## Conclusion - -The Codebase Community Template database is a **well-structured, comprehensive Q&A platform** that captures all essential aspects of community-driven knowledge sharing. With over 885K records across 8 interconnected tables, it provides rich opportunities for: - -- **User behavior analysis** - Reputation, engagement, retention -- **Content quality assessment** - Answer rates, acceptance, views -- **Community health monitoring** - Activity trends, voting patterns -- **Expertise discovery** - Top contributors by tag/topic -- **Platform optimization** - Response times, closure rates - -The database is **production-ready** and suitable for building analytics dashboards, recommendation systems, and community management tools. The 25 defined metrics and 40 question templates provide immediate value for data analysis and natural language query interfaces. - ---- - -## Deliverables Summary - -✅ **Database Discovery Complete** - -**Artifacts Created**: -1. `/tmp/codebase_community_discovery.md` - Complete technical discovery -2. `/tmp/metrics_and_kpis.sql` - 25 production-ready metric queries -3. `/tmp/question_templates.md` - 40 NL-to-SQL question templates -4. `/tmp/global_database_summary.md` - This comprehensive summary - -**Coverage Achieved**: -- ✅ 8 tables fully analyzed and documented -- ✅ 14 foreign key relationships mapped -- ✅ 5 domains defined with entities and roles -- ✅ 25 metrics/KPIs with SQL implementations -- ✅ 40 question templates with examples -- ✅ Complete join patterns documented -- ✅ Data quality insights included - -**Database Statistics**: -- Total records: ~885,000 -- Tables: 8 -- Relationships: 14 FKs -- Time span: 2010-present -- Schema: codebase_community_template - ---- - -*Discovery completed using MCP catalog tools and direct SQL analysis* -*Run ID: 7* -*Model: claude-3.5-sonnet* -*Date: 2025* diff --git a/scripts/mcp/DiscoveryAgent/ClaudeCode_Headless/tmp/question_templates.md b/scripts/mcp/DiscoveryAgent/ClaudeCode_Headless/tmp/question_templates.md deleted file mode 100644 index 560208a6d..000000000 --- a/scripts/mcp/DiscoveryAgent/ClaudeCode_Headless/tmp/question_templates.md +++ /dev/null @@ -1,1474 +0,0 @@ -# Codebase Community Database - 40 Question Templates - -## Template Structure -Each template includes: -- **Natural Language Question**: How users would ask it -- **SQL Template**: Parameterized query structure -- **Example SQL**: Concrete implementation -- **Domain**: Business domain classification -- **Complexity**: Simple/Medium/Complex - ---- - -## USER ANALYTICS TEMPLATES (10 questions) - -### Template 1: Top Users by Reputation -**Natural Language**: "Who are the top N users by reputation?" -**Domain**: User Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - Id AS user_id, - DisplayName, - Reputation, - Views AS profile_views, - UpVotes, - DownVotes -FROM codebase_community_template.users -WHERE Reputation > 0 -ORDER BY Reputation DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -SELECT Id, DisplayName, Reputation, Views, UpVotes, DownVotes -FROM codebase_community_template.users -WHERE Reputation > 0 -ORDER BY Reputation DESC -LIMIT 10; -``` - ---- - -### Template 2: User Activity Summary -**Natural Language**: "What is the activity summary for user {{user_id}}?" -**Domain**: User Analytics -**Complexity**: Medium - -**SQL Template**: -```sql -SELECT - u.Id, - u.DisplayName, - u.Reputation, - COUNT(DISTINCT p.Id) AS post_count, - COUNT(DISTINCT c.Id) AS comment_count, - COUNT(DISTINCT v.Id) AS vote_count, - COUNT(DISTINCT b.Id) AS badge_count -FROM codebase_community_template.users u -LEFT JOIN codebase_community_template.posts p ON u.Id = p.OwnerUserId -LEFT JOIN codebase_community_template.comments c ON u.Id = c.UserId -LEFT JOIN codebase_community_template.votes v ON u.Id = v.UserId -LEFT JOIN codebase_community_template.badges b ON u.Id = b.UserId -WHERE u.Id = {{user_id}} -GROUP BY u.Id, u.DisplayName, u.Reputation; -``` - -**Example**: -```sql -SELECT u.Id, u.DisplayName, u.Reputation, - COUNT(DISTINCT p.Id) AS post_count, - COUNT(DISTINCT c.Id) AS comment_count, - COUNT(DISTINCT v.Id) AS vote_count, - COUNT(DISTINCT b.Id) AS badge_count -FROM codebase_community_template.users u -LEFT JOIN codebase_community_template.posts p ON u.Id = p.OwnerUserId -LEFT JOIN codebase_community_template.comments c ON u.Id = c.UserId -LEFT JOIN codebase_community_template.votes v ON u.Id = v.UserId -LEFT JOIN codebase_community_template.badges b ON u.Id = b.UserId -WHERE u.Id = 8 -GROUP BY u.Id, u.DisplayName, u.Reputation; -``` - ---- - -### Template 3: User Registration Trends -**Natural Language**: "How many users joined each month in {{year}}?" -**Domain**: User Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - DATE_FORMAT(CreationDate, '%Y-%m') AS month, - COUNT(*) AS new_users -FROM codebase_community_template.users -WHERE YEAR(CreationDate) = {{year}} -GROUP BY DATE_FORMAT(CreationDate, '%Y-%m') -ORDER BY month; -``` - -**Example**: -```sql -SELECT - DATE_FORMAT(CreationDate, '%Y-%m') AS month, - COUNT(*) AS new_users -FROM codebase_community_template.users -WHERE YEAR(CreationDate) = 2010 -GROUP BY DATE_FORMAT(CreationDate, '%Y-%m') -ORDER BY month; -``` - ---- - -### Template 4: Most Active Users by Posts -**Natural Language**: "Who are the most active users in the past {{days}} days?" -**Domain**: User Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - u.Id, - u.DisplayName, - COUNT(p.Id) AS post_count -FROM codebase_community_template.users u -INNER JOIN codebase_community_template.posts p ON u.Id = p.OwnerUserId -WHERE p.CreaionDate >= DATE_SUB(CURDATE(), INTERVAL {{days}} DAY) -GROUP BY u.Id, u.DisplayName -ORDER BY post_count DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -SELECT u.Id, u.DisplayName, COUNT(p.Id) AS post_count -FROM codebase_community_template.users u -INNER JOIN codebase_community_template.posts p ON u.Id = p.OwnerUserId -WHERE p.CreaionDate >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) -GROUP BY u.Id, u.DisplayName -ORDER BY post_count DESC -LIMIT 10; -``` - ---- - -### Template 5: User Answer Acceptance Rate -**Natural Language**: "What is the answer acceptance rate for users with at least {{min_answers}} answers?" -**Domain**: User Analytics -**Complexity**: Medium - -**SQL Template**: -```sql -WITH user_answers AS ( - SELECT - a.OwnerUserId, - COUNT(*) AS total_answers, - SUM(CASE WHEN q.AcceptedAnswerId = a.Id THEN 1 ELSE 0 END) AS accepted_answers - FROM codebase_community_template.posts a - INNER JOIN codebase_community_template.posts q ON a.ParentId = q.Id - WHERE a.PostTypeId = 2 - AND q.PostTypeId = 1 - AND a.OwnerUserId IS NOT NULL - GROUP BY a.OwnerUserId - HAVING COUNT(*) >= {{min_answers}} -) -SELECT - u.DisplayName, - ua.total_answers, - ua.accepted_answers, - ROUND(ua.accepted_answers * 100.0 / ua.total_answers, 2) AS acceptance_rate_pct -FROM user_answers ua -INNER JOIN codebase_community_template.users u ON ua.OwnerUserId = u.Id -ORDER BY acceptance_rate_pct DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -WITH user_answers AS ( - SELECT - a.OwnerUserId, - COUNT(*) AS total_answers, - SUM(CASE WHEN q.AcceptedAnswerId = a.Id THEN 1 ELSE 0 END) AS accepted_answers - FROM codebase_community_template.posts a - INNER JOIN codebase_community_template.posts q ON a.ParentId = q.Id - WHERE a.PostTypeId = 2 AND q.PostTypeId = 1 AND a.OwnerUserId IS NOT NULL - GROUP BY a.OwnerUserId - HAVING COUNT(*) >= 10 -) -SELECT - u.DisplayName, - ua.total_answers, - ua.accepted_answers, - ROUND(ua.accepted_answers * 100.0 / ua.total_answers, 2) AS acceptance_rate_pct -FROM user_answers ua -INNER JOIN codebase_community_template.users u ON ua.OwnerUserId = u.Id -ORDER BY acceptance_rate_pct DESC -LIMIT 20; -``` - ---- - -### Template 6: Users by Reputation Range -**Natural Language**: "How many users have reputation between {{min_rep}} and {{max_rep}}?" -**Domain**: User Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - COUNT(*) AS user_count -FROM codebase_community_template.users -WHERE Reputation >= {{min_rep}} AND Reputation <= {{max_rep}}; -``` - -**Example**: -```sql -SELECT COUNT(*) AS user_count -FROM codebase_community_template.users -WHERE Reputation >= 100 AND Reputation <= 500; -``` - ---- - -### Template 7: User Badges Summary -**Natural Language**: "What badges has user {{user_id}} earned?" -**Domain**: User Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - b.Name AS badge_name, - b.[Date] AS earned_date, - u.DisplayName -FROM codebase_community_template.badges b -INNER JOIN codebase_community_template.users u ON b.UserId = u.Id -WHERE b.UserId = {{user_id}} -ORDER BY b.[Date] DESC; -``` - -**Example**: -```sql -SELECT b.Name AS badge_name, b.[Date] AS earned_date, u.DisplayName -FROM codebase_community_template.badges b -INNER JOIN codebase_community_template.users u ON b.UserId = u.Id -WHERE b.UserId = 8 -ORDER BY b.[Date] DESC; -``` - ---- - -### Template 8: Top Badge Earners -**Natural Language**: "Who has earned the most badges?" -**Domain**: User Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - u.Id, - u.DisplayName, - COUNT(b.Id) AS badge_count -FROM codebase_community_template.users u -INNER JOIN codebase_community_template.badges b ON u.Id = b.UserId -GROUP BY u.Id, u.DisplayName -ORDER BY badge_count DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -SELECT u.Id, u.DisplayName, COUNT(b.Id) AS badge_count -FROM codebase_community_template.users u -INNER JOIN codebase_community_template.badges b ON u.Id = b.UserId -GROUP BY u.Id, u.DisplayName -ORDER BY badge_count DESC -LIMIT 20; -``` - ---- - -### Template 9: User Voting Behavior -**Natural Language**: "What is the voting behavior for user {{user_id}}?" -**Domain**: User Analytics -**Complexity**: Medium - -**SQL Template**: -```sql -SELECT - u.DisplayName, - u.UpVotes, - u.DownVotes, - (u.UpVotes + u.DownVotes) AS total_votes, - CASE - WHEN (u.UpVotes + u.DownVotes) > 0 - THEN ROUND(u.UpVotes * 100.0 / (u.UpVotes + u.DownVotes), 2) - ELSE 0 - END AS upvote_percentage -FROM codebase_community_template.users u -WHERE u.Id = {{user_id}}; -``` - -**Example**: -```sql -SELECT u.DisplayName, u.UpVotes, u.DownVotes, - (u.UpVotes + u.DownVotes) AS total_votes, - CASE WHEN (u.UpVotes + u.DownVotes) > 0 - THEN ROUND(u.UpVotes * 100.0 / (u.UpVotes + u.DownVotes), 2) - ELSE 0 - END AS upvote_percentage -FROM codebase_community_template.users u -WHERE u.Id = 8; -``` - ---- - -### Template 10: User Geographic Distribution -**Natural Language**: "How many users are from each location?" -**Domain**: User Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - Location, - COUNT(*) AS user_count -FROM codebase_community_template.users -WHERE Location IS NOT NULL AND Location != '' -GROUP BY Location -ORDER BY user_count DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -SELECT Location, COUNT(*) AS user_count -FROM codebase_community_template.users -WHERE Location IS NOT NULL AND Location != '' -GROUP BY Location -ORDER BY user_count DESC -LIMIT 20; -``` - ---- - -## CONTENT ANALYTICS TEMPLATES (10 questions) - -### Template 11: Most Viewed Questions -**Natural Language**: "What are the most viewed questions about {{tag}}?" -**Domain**: Content Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - Id, - Title, - ViewCount, - Score, - AnswerCount, - CreaionDate -FROM codebase_community_template.posts -WHERE PostTypeId = 1 - AND Tags LIKE '%<{{tag}}>%' -ORDER BY ViewCount DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -SELECT Id, Title, ViewCount, Score, AnswerCount, CreaionDate -FROM codebase_community_template.posts -WHERE PostTypeId = 1 AND Tags LIKE '%%' -ORDER BY ViewCount DESC -LIMIT 10; -``` - ---- - -### Template 12: Questions Without Answers -**Natural Language**: "What questions about {{tag}} have no answers?" -**Domain**: Content Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - Id, - Title, - CreaionDate, - ViewCount, - Score -FROM codebase_community_template.posts -WHERE PostTypeId = 1 - AND AnswerCount = 0 - AND Tags LIKE '%<{{tag}}>%' -ORDER BY CreaionDate DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -SELECT Id, Title, CreaionDate, ViewCount, Score -FROM codebase_community_template.posts -WHERE PostTypeId = 1 AND AnswerCount = 0 AND Tags LIKE '%%' -ORDER BY CreaionDate DESC -LIMIT 20; -``` - ---- - -### Template 13: Highest Scored Posts -**Natural Language**: "What are the highest scored posts in the past {{days}} days?" -**Domain**: Content Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - Id, - CASE - WHEN PostTypeId = 1 THEN Title - ELSE 'Answer' - END AS title, - PostTypeId, - Score, - ViewCount, - CreaionDate -FROM codebase_community_template.posts -WHERE CreaionDate >= DATE_SUB(CURDATE(), INTERVAL {{days}} DAY) -ORDER BY Score DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -SELECT Id, - CASE WHEN PostTypeId = 1 THEN Title ELSE 'Answer' END AS title, - PostTypeId, Score, ViewCount, CreaionDate -FROM codebase_community_template.posts -WHERE CreaionDate >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) -ORDER BY Score DESC -LIMIT 20; -``` - ---- - -### Template 14: Questions by Time Period -**Natural Language**: "How many questions were created per day in the last {{days}} days?" -**Domain**: Content Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - DATE(CreaionDate) AS question_date, - COUNT(*) AS question_count -FROM codebase_community_template.posts -WHERE PostTypeId = 1 - AND CreaionDate >= DATE_SUB(CURDATE(), INTERVAL {{days}} DAY) -GROUP BY DATE(CreaionDate) -ORDER BY question_date DESC; -``` - -**Example**: -```sql -SELECT DATE(CreaionDate) AS question_date, COUNT(*) AS question_count -FROM codebase_community_template.posts -WHERE PostTypeId = 1 - AND CreaionDate >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) -GROUP BY DATE(CreaionDate) -ORDER BY question_date DESC; -``` - ---- - -### Template 15: Answer Quality Comparison -**Natural Language**: "How do accepted answers compare to non-accepted answers for {{tag}} questions?" -**Domain**: Content Analytics -**Complexity**: Medium - -**SQL Template**: -```sql -WITH answer_stats AS ( - SELECT - a.Id, - a.Score, - CASE WHEN q.AcceptedAnswerId = a.Id THEN 'accepted' ELSE 'not_accepted' END AS status - FROM codebase_community_template.posts a - INNER JOIN codebase_community_template.posts q ON a.ParentId = q.Id - WHERE a.PostTypeId = 2 - AND q.PostTypeId = 1 - AND q.Tags LIKE '%<{{tag}}>%' -) -SELECT - status, - COUNT(*) AS answer_count, - ROUND(AVG(Score), 2) AS avg_score, - SUM(CASE WHEN Score > 0 THEN 1 ELSE 0 END) AS positive_count -FROM answer_stats -GROUP BY status; -``` - -**Example**: -```sql -WITH answer_stats AS ( - SELECT - a.Id, - a.Score, - CASE WHEN q.AcceptedAnswerId = a.Id THEN 'accepted' ELSE 'not_accepted' END AS status - FROM codebase_community_template.posts a - INNER JOIN codebase_community_template.posts q ON a.ParentId = q.Id - WHERE a.PostTypeId = 2 AND q.PostTypeId = 1 AND q.Tags LIKE '%%' -) -SELECT - status, - COUNT(*) AS answer_count, - ROUND(AVG(Score), 2) AS avg_score, - SUM(CASE WHEN Score > 0 THEN 1 ELSE 0 END) AS positive_count -FROM answer_stats -GROUP BY status; -``` - ---- - -### Template 16: Average Answer Count -**Natural Language**: "What is the average number of answers per question for {{tag}}?" -**Domain**: Content Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - ROUND(AVG(AnswerCount), 2) AS avg_answers, - ROUND(PERCENTILE_CONT(0.50) OVER (), 2) AS median_answers, - ROUND(PERCENTILE_CONT(0.75) OVER (), 2) AS p75_answers, - COUNT(*) AS total_questions -FROM codebase_community_template.posts -WHERE PostTypeId = 1 - AND Tags LIKE '%<{{tag}}>%'; -``` - -**Example**: -```sql -SELECT ROUND(AVG(AnswerCount), 2) AS avg_answers, - ROUND(PERCENTILE_CONT(0.50) OVER (), 2) AS median_answers, - ROUND(PERCENTILE_CONT(0.75) OVER (), 2) AS p75_answers, - COUNT(*) AS total_questions -FROM codebase_community_template.posts -WHERE PostTypeId = 1 AND Tags LIKE '%%'; -``` - ---- - -### Template 17: Questions with Most Answers -**Natural Language**: "What questions about {{tag}} have the most answers?" -**Domain**: Content Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - Id, - Title, - AnswerCount, - ViewCount, - Score, - AcceptedAnswerId, - CreaionDate -FROM codebase_community_template.posts -WHERE PostTypeId = 1 - AND Tags LIKE '%<{{tag}}>%' - AND AnswerCount > 0 -ORDER BY AnswerCount DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -SELECT Id, Title, AnswerCount, ViewCount, Score, AcceptedAnswerId, CreaionDate -FROM codebase_community_template.posts -WHERE PostTypeId = 1 AND Tags LIKE '%%' -ORDER BY AnswerCount DESC -LIMIT 10; -``` - ---- - -### Template 18: Post Edit History -**Natural Language**: "What is the edit history for post {{post_id}}?" -**Domain**: Content Analytics -**Complexity**: Medium - -**SQL Template**: -```sql -SELECT - ph.Id, - ph.PostHistoryTypeId, - ph.CreationDate, - u.DisplayName AS editor_name, - ph.Text, - ph.Comment -FROM codebase_community_template.postHistory ph -LEFT JOIN codebase_community_template.users u ON ph.UserId = u.Id -WHERE ph.PostId = {{post_id}} -ORDER BY ph.CreationDate ASC; -``` - -**Example**: -```sql -SELECT ph.Id, ph.PostHistoryTypeId, ph.CreationDate, - u.DisplayName AS editor_name, ph.Text, ph.Comment -FROM codebase_community_template.postHistory ph -LEFT JOIN codebase_community_template.users u ON ph.UserId = u.Id -WHERE ph.PostId = 1 -ORDER BY ph.CreationDate ASC; -``` - ---- - -### Template 19: Related Questions -**Natural Language**: "What questions are related to post {{post_id}}?" -**Domain**: Content Analytics -**Complexity**: Medium - -**SQL Template**: -```sql -SELECT - pl.Id AS link_id, - pl.CreationDate AS link_date, - pl.LinkTypeId, - p_rel.Id AS related_post_id, - p_rel.Title AS related_title, - p_rel.Score AS related_score, - p_rel.AnswerCount -FROM codebase_community_template.postLinks pl -INNER JOIN codebase_community_template.posts p_rel ON pl.RelatedPostId = p_rel.Id -WHERE pl.PostId = {{post_id}} -ORDER BY pl.CreationDate DESC; -``` - -**Example**: -```sql -SELECT pl.Id AS link_id, pl.CreationDate AS link_date, pl.LinkTypeId, - p_rel.Id AS related_post_id, p_rel.Title AS related_title, - p_rel.Score AS related_score, p_rel.AnswerCount -FROM codebase_community_template.postLinks pl -INNER JOIN codebase_community_template.posts p_rel ON pl.RelatedPostId = p_rel.Id -WHERE pl.PostId = 1 -ORDER BY pl.CreationDate DESC; -``` - ---- - -### Template 20: Community Wiki Posts -**Natural Language**: "What posts have become community wikis?" -**Domain**: Content Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - p.Id, - CASE WHEN p.PostTypeId = 1 THEN p.Title ELSE 'Answer' END AS title, - p.PostTypeId, - p.CommunityOwnedDate, - p.Score, - u.DisplayName AS original_author -FROM codebase_community_template.posts p -INNER JOIN codebase_community_template.users u ON p.OwnerUserId = u.Id -WHERE p.CommunityOwnedDate IS NOT NULL -ORDER BY p.CommunityOwnedDate DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -SELECT p.Id, - CASE WHEN p.PostTypeId = 1 THEN p.Title ELSE 'Answer' END AS title, - p.PostTypeId, p.CommunityOwnedDate, p.Score, - u.DisplayName AS original_author -FROM codebase_community_template.posts p -INNER JOIN codebase_community_template.users u ON p.OwnerUserId = u.Id -WHERE p.CommunityOwnedDate IS NOT NULL -ORDER BY p.CommunityOwnedDate DESC -LIMIT 20; -``` - ---- - -## ENGAGEMENT ANALYTICS TEMPLATES (10 questions) - -### Template 21: Most Commented Posts -**Natural Language**: "What posts have the most comments?" -**Domain**: Engagement Analytics -**Complexity**: Medium - -**SQL Template**: -```sql -SELECT - p.Id, - CASE WHEN p.PostTypeId = 1 THEN p.Title ELSE 'Answer' END AS title, - p.PostTypeId, - COUNT(c.Id) AS comment_count, - p.Score, - p.ViewCount -FROM codebase_community_template.posts p -INNER JOIN codebase_community_template.comments c ON p.Id = c.PostId -GROUP BY p.Id, p.Title, p.PostTypeId, p.Score, p.ViewCount -ORDER BY comment_count DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -SELECT p.Id, - CASE WHEN p.PostTypeId = 1 THEN p.Title ELSE 'Answer' END AS title, - p.PostTypeId, COUNT(c.Id) AS comment_count, p.Score, p.ViewCount -FROM codebase_community_template.posts p -INNER JOIN codebase_community_template.comments c ON p.Id = c.PostId -GROUP BY p.Id, p.Title, p.PostTypeId, p.Score, p.ViewCount -ORDER BY comment_count DESC -LIMIT 20; -``` - ---- - -### Template 22: Top Commenters -**Natural Language**: "Who are the most active commenters?" -**Domain**: Engagement Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - u.Id, - u.DisplayName, - COUNT(c.Id) AS comment_count -FROM codebase_community_template.users u -INNER JOIN codebase_community_template.comments c ON u.Id = c.UserId -GROUP BY u.Id, u.DisplayName -ORDER BY comment_count DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -SELECT u.Id, u.DisplayName, COUNT(c.Id) AS comment_count -FROM codebase_community_template.users u -INNER JOIN codebase_community_template.comments c ON u.Id = c.UserId -GROUP BY u.Id, u.DisplayName -ORDER BY comment_count DESC -LIMIT 20; -``` - ---- - -### Template 23: Voting Trends -**Natural Language**: "How many votes were cast per day in the last {{days}} days?" -**Domain**: Engagement Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - CreationDate AS vote_date, - COUNT(*) AS vote_count, - SUM(CASE WHEN VoteTypeId = 2 THEN 1 ELSE 0 END) AS upvotes, - SUM(CASE WHEN VoteTypeId = 3 THEN 1 ELSE 0 END) AS downvotes -FROM codebase_community_template.votes -WHERE CreationDate >= DATE_SUB(CURDATE(), INTERVAL {{days}} DAY) -GROUP BY CreationDate -ORDER BY vote_date DESC; -``` - -**Example**: -```sql -SELECT CreationDate AS vote_date, COUNT(*) AS vote_count, - SUM(CASE WHEN VoteTypeId = 2 THEN 1 ELSE 0 END) AS upvotes, - SUM(CASE WHEN VoteTypeId = 3 THEN 1 ELSE 0 END) AS downvotes -FROM codebase_community_template.votes -WHERE CreationDate >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) -GROUP BY CreationDate -ORDER BY vote_date DESC; -``` - ---- - -### Template 24: Post Vote Distribution -**Natural Language**: "What is the vote distribution for post {{post_id}}?" -**Domain**: Engagement Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - VoteTypeId, - COUNT(*) AS vote_count -FROM codebase_community_template.votes -WHERE PostId = {{post_id}} -GROUP BY VoteTypeId -ORDER BY vote_count DESC; -``` - -**Example**: -```sql -SELECT VoteTypeId, COUNT(*) AS vote_count -FROM codebase_community_template.votes -WHERE PostId = 1 -GROUP BY VoteTypeId -ORDER BY vote_count DESC; -``` - ---- - -### Template 25: Most Voted Posts -**Natural Language**: "What posts have received the most votes?" -**Domain**: Engagement Analytics -**Complexity**: Medium - -**SQL Template**: -```sql -SELECT - p.Id, - CASE WHEN p.PostTypeId = 1 THEN p.Title ELSE 'Answer' END AS title, - p.PostTypeId, - COUNT(v.Id) AS vote_count, - SUM(CASE WHEN v.VoteTypeId = 2 THEN 1 ELSE 0 END) AS upvotes, - SUM(CASE WHEN v.VoteTypeId = 3 THEN 1 ELSE 0 END) AS downvotes, - p.Score -FROM codebase_community_template.posts p -INNER JOIN codebase_community_template.votes v ON p.Id = v.PostId -GROUP BY p.Id, p.Title, p.PostTypeId, p.Score -ORDER BY vote_count DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -SELECT p.Id, - CASE WHEN p.PostTypeId = 1 THEN p.Title ELSE 'Answer' END AS title, - p.PostTypeId, COUNT(v.Id) AS vote_count, - SUM(CASE WHEN v.VoteTypeId = 2 THEN 1 ELSE 0 END) AS upvotes, - SUM(CASE WHEN v.VoteTypeId = 3 THEN 1 ELSE 0 END) AS downvotes, p.Score -FROM codebase_community_template.posts p -INNER JOIN codebase_community_template.votes v ON p.Id = v.PostId -GROUP BY p.Id, p.Title, p.PostTypeId, p.Score -ORDER BY vote_count DESC -LIMIT 20; -``` - ---- - -### Template 26: User Comment Activity -**Natural Language**: "What comments has user {{user_id}} made?" -**Domain**: Engagement Analytics -**Complexity**: Medium - -**SQL Template**: -```sql -SELECT - c.Id, - c.Text, - c.Score, - c.CreationDate, - p.Id AS post_id, - CASE WHEN p.PostTypeId = 1 THEN p.Title ELSE 'Answer' END AS post_title -FROM codebase_community_template.comments c -INNER JOIN codebase_community_template.posts p ON c.PostId = p.Id -WHERE c.UserId = {{user_id}} -ORDER BY c.CreationDate DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -SELECT c.Id, c.Text, c.Score, c.CreationDate, - p.Id AS post_id, - CASE WHEN p.PostTypeId = 1 THEN p.Title ELSE 'Answer' END AS post_title -FROM codebase_community_template.comments c -INNER JOIN codebase_community_template.posts p ON c.PostId = p.Id -WHERE c.UserId = 8 -ORDER BY c.CreationDate DESC -LIMIT 20; -``` - ---- - -### Template 27: Comment Sentiment Analysis -**Natural Language**: "What is the score distribution of comments on post {{post_id}}?" -**Domain**: Engagement Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - Score, - COUNT(*) AS comment_count -FROM codebase_community_template.comments -WHERE PostId = {{post_id}} -GROUP BY Score -ORDER BY Score DESC; -``` - -**Example**: -```sql -SELECT Score, COUNT(*) AS comment_count -FROM codebase_community_template.comments -WHERE PostId = 1 -GROUP BY Score -ORDER BY Score DESC; -``` - ---- - -### Template 28: Recent Activity on Post -**Natural Language**: "What is the recent activity (comments and votes) on post {{post_id}}?" -**Domain**: Engagement Analytics -**Complexity**: Complex - -**SQL Template**: -```sql -SELECT - 'comment' AS activity_type, - c.Id, - c.CreationDate, - c.Score, - u.DisplayName AS user_name, - c.Text -FROM codebase_community_template.comments c -INNER JOIN codebase_community_template.users u ON c.UserId = u.Id -WHERE c.PostId = {{post_id}} - -UNION ALL - -SELECT - 'vote' AS activity_type, - v.Id, - v.CreationDate, - CASE WHEN v.VoteTypeId = 2 THEN 1 ELSE -1 END AS Score, - u.DisplayName AS user_name, - CAST(v.VoteTypeId AS CHAR) AS Text -FROM codebase_community_template.votes v -INNER JOIN codebase_community_template.users u ON v.UserId = u.Id -WHERE v.PostId = {{post_id}} - -ORDER BY CreationDate DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -SELECT 'comment' AS activity_type, c.Id, c.CreationDate, c.Score, - u.DisplayName AS user_name, c.Text -FROM codebase_community_template.comments c -INNER JOIN codebase_community_template.users u ON c.UserId = u.Id -WHERE c.PostId = 1 - -UNION ALL - -SELECT 'vote' AS activity_type, v.Id, v.CreationDate, - CASE WHEN v.VoteTypeId = 2 THEN 1 ELSE -1 END AS Score, - u.DisplayName AS user_name, CAST(v.VoteTypeId AS CHAR) AS Text -FROM codebase_community_template.votes v -INNER JOIN codebase_community_template.users u ON v.UserId = u.Id -WHERE v.PostId = 1 - -ORDER BY CreationDate DESC -LIMIT 50; -``` - ---- - -### Template 29: Engagement Rate by User -**Natural Language**: "What is the engagement rate (comments + votes per post) for user {{user_id}}?" -**Domain**: Engagement Analytics -**Complexity**: Medium - -**SQL Template**: -```sql -SELECT - u.DisplayName, - COUNT(DISTINCT p.Id) AS post_count, - COUNT(DISTINCT c.Id) AS comments_received, - COUNT(DISTINCT v.Id) AS votes_received, - ROUND(COUNT(DISTINCT c.Id) * 1.0 / NULLIF(COUNT(DISTINCT p.Id), 0), 2) AS avg_comments_per_post, - ROUND(COUNT(DISTINCT v.Id) * 1.0 / NULLIF(COUNT(DISTINCT p.Id), 0), 2) AS avg_votes_per_post -FROM codebase_community_template.users u -INNER JOIN codebase_community_template.posts p ON u.Id = p.OwnerUserId -LEFT JOIN codebase_community_template.comments c ON p.Id = c.PostId -LEFT JOIN codebase_community_template.votes v ON p.Id = v.PostId -WHERE u.Id = {{user_id}} -GROUP BY u.DisplayName; -``` - -**Example**: -```sql -SELECT u.DisplayName, - COUNT(DISTINCT p.Id) AS post_count, - COUNT(DISTINCT c.Id) AS comments_received, - COUNT(DISTINCT v.Id) AS votes_received, - ROUND(COUNT(DISTINCT c.Id) * 1.0 / NULLIF(COUNT(DISTINCT p.Id), 0), 2) AS avg_comments_per_post, - ROUND(COUNT(DISTINCT v.Id) * 1.0 / NULLIF(COUNT(DISTINCT p.Id), 0), 2) AS avg_votes_per_post -FROM codebase_community_template.users u -INNER JOIN codebase_community_template.posts p ON u.Id = p.OwnerUserId -LEFT JOIN codebase_community_template.comments c ON p.Id = c.PostId -LEFT JOIN codebase_community_template.votes v ON p.Id = v.PostId -WHERE u.Id = 8 -GROUP BY u.DisplayName; -``` - ---- - -### Template 30: Most Active Voters -**Natural Language**: "Who are the most active voters?" -**Domain**: Engagement Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - u.Id, - u.DisplayName, - COUNT(v.Id) AS vote_count, - SUM(CASE WHEN v.VoteTypeId = 2 THEN 1 ELSE 0 END) AS upvotes_cast, - SUM(CASE WHEN v.VoteTypeId = 3 THEN 1 ELSE 0 END) AS downvotes_cast -FROM codebase_community_template.users u -INNER JOIN codebase_community_template.votes v ON u.Id = v.UserId -GROUP BY u.Id, u.DisplayName -ORDER BY vote_count DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -SELECT u.Id, u.DisplayName, COUNT(v.Id) AS vote_count, - SUM(CASE WHEN v.VoteTypeId = 2 THEN 1 ELSE 0 END) AS upvotes_cast, - SUM(CASE WHEN v.VoteTypeId = 3 THEN 1 ELSE 0 END) AS downvotes_cast -FROM codebase_community_template.users u -INNER JOIN codebase_community_template.votes v ON u.Id = v.UserId -GROUP BY u.Id, u.DisplayName -ORDER BY vote_count DESC -LIMIT 20; -``` - ---- - -## TAG ANALYTICS TEMPLATES (10 questions) - -### Template 31: Tag Usage Statistics -**Natural Language**: "What are the most popular tags?" -**Domain**: Tag Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - TagName, - Count AS usage_count, - ROUND(Count * 100.0 / (SELECT SUM(Count) FROM codebase_community_template.tags), 2) AS percentage -FROM codebase_community_template.tags -ORDER BY Count DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -SELECT TagName, Count AS usage_count, - ROUND(Count * 100.0 / (SELECT SUM(Count) FROM codebase_community_template.tags), 2) AS percentage -FROM codebase_community_template.tags -ORDER BY Count DESC -LIMIT 20; -``` - ---- - -### Template 32: Questions by Multiple Tags -**Natural Language**: "What questions have both {{tag1}} and {{tag2}}?" -**Domain**: Tag Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - Id, - Title, - Tags, - Score, - AnswerCount, - ViewCount, - CreaionDate -FROM codebase_community_template.posts -WHERE PostTypeId = 1 - AND Tags LIKE '%<{{tag1}}>%' - AND Tags LIKE '%<{{tag2}}>%' -ORDER BY Score DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -SELECT Id, Title, Tags, Score, AnswerCount, ViewCount, CreaionDate -FROM codebase_community_template.posts -WHERE PostTypeId = 1 - AND Tags LIKE '%%' - AND Tags LIKE '%%' -ORDER BY Score DESC -LIMIT 20; -``` - ---- - -### Template 33: Tag Expertise Leaders -**Natural Language**: "Who are the top experts for {{tag}}?" -**Domain**: Tag Analytics -**Complexity**: Medium - -**SQL Template**: -```sql -WITH tag_experts AS ( - SELECT - a.OwnerUserId, - COUNT(*) AS answer_count, - SUM(a.Score) AS total_score, - AVG(a.Score) AS avg_score - FROM codebase_community_template.posts a - INNER JOIN codebase_community_template.posts q ON a.ParentId = q.Id - WHERE a.PostTypeId = 2 -- Answers - AND q.PostTypeId = 1 -- Questions - AND q.Tags LIKE '%<{{tag}}>%' - AND a.OwnerUserId IS NOT NULL - GROUP BY a.OwnerUserId - HAVING answer_count >= {{min_answers}} -) -SELECT - u.DisplayName, - te.answer_count, - te.total_score, - ROUND(te.avg_score, 2) AS avg_score_per_answer -FROM tag_experts te -INNER JOIN codebase_community_template.users u ON te.OwnerUserId = u.Id -ORDER BY total_score DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -WITH tag_experts AS ( - SELECT - a.OwnerUserId, - COUNT(*) AS answer_count, - SUM(a.Score) AS total_score, - AVG(a.Score) AS avg_score - FROM codebase_community_template.posts a - INNER JOIN codebase_community_template.posts q ON a.ParentId = q.Id - WHERE a.PostTypeId = 2 AND q.PostTypeId = 1 - AND q.Tags LIKE '%%' - AND a.OwnerUserId IS NOT NULL - GROUP BY a.OwnerUserId - HAVING answer_count >= 5 -) -SELECT u.DisplayName, te.answer_count, te.total_score, - ROUND(te.avg_score, 2) AS avg_score_per_answer -FROM tag_experts te -INNER JOIN codebase_community_template.users u ON te.OwnerUserId = u.Id -ORDER BY total_score DESC -LIMIT 10; -``` - ---- - -### Template 34: Unanswered Questions by Tag -**Natural Language**: "What tags have the highest percentage of unanswered questions?" -**Domain**: Tag Analytics -**Complexity**: Complex - -**SQL Template**: -```sql -WITH tag_unanswered AS ( - SELECT - SUBSTRING_INDEX(SUBSTRING_INDEX(Tags, '><', n.n), '>', -1) AS tag_name, - COUNT(*) AS total_questions, - SUM(CASE WHEN AnswerCount = 0 THEN 1 ELSE 0 END) AS unanswered_count - FROM codebase_community_template.posts p - CROSS JOIN ( - SELECT 1 AS n UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL - SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL - SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 - ) n - WHERE p.PostTypeId = 1 - AND p.Tags LIKE '<%>' - AND n.n <= LENGTH(p.Tags) - LENGTH(REPLACE(p.Tags, '><', '')) + 1 - GROUP BY tag_name - HAVING total_questions >= {{min_questions}} -) -SELECT - tag_name, - total_questions, - unanswered_count, - ROUND(unanswered_count * 100.0 / total_questions, 2) AS unanswered_percentage -FROM tag_unanswered -ORDER BY unanswered_percentage DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -WITH tag_unanswered AS ( - SELECT - SUBSTRING_INDEX(SUBSTRING_INDEX(Tags, '><', n.n), '>', -1) AS tag_name, - COUNT(*) AS total_questions, - SUM(CASE WHEN AnswerCount = 0 THEN 1 ELSE 0 END) AS unanswered_count - FROM codebase_community_template.posts p - CROSS JOIN ( - SELECT 1 AS n UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL - SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL - SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 - ) n - WHERE p.PostTypeId = 1 AND p.Tags LIKE '<%>' - AND n.n <= LENGTH(p.Tags) - LENGTH(REPLACE(p.Tags, '><', '')) + 1 - GROUP BY tag_name - HAVING total_questions >= 10 -) -SELECT - tag_name, - total_questions, - unanswered_count, - ROUND(unanswered_count * 100.0 / total_questions, 2) AS unanswered_percentage -FROM tag_unanswered -ORDER BY unanswered_percentage DESC -LIMIT 20; -``` - ---- - -### Template 35: Tag Growth Trend -**Natural Language**: "How has {{tag}} usage changed over the last {{months}} months?" -**Domain**: Tag Analytics -**Complexity**: Medium - -**SQL Template**: -```sql -SELECT - DATE_FORMAT(CreaionDate, '%Y-%m') AS month, - COUNT(*) AS question_count -FROM codebase_community_template.posts -WHERE PostTypeId = 1 - AND Tags LIKE '%<{{tag}}>%' - AND CreaionDate >= DATE_SUB(CURDATE(), INTERVAL {{months}} MONTH) -GROUP BY DATE_FORMAT(CreaionDate, '%Y-%m') -ORDER BY month; -``` - -**Example**: -```sql -SELECT DATE_FORMAT(CreaionDate, '%Y-%m') AS month, COUNT(*) AS question_count -FROM codebase_community_template.posts -WHERE PostTypeId = 1 - AND Tags LIKE '%%' - AND CreaionDate >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH) -GROUP BY DATE_FORMAT(CreaionDate, '%Y-%m') -ORDER BY month; -``` - ---- - -### Template 36: Related Tags -**Natural Language**: "What tags are commonly used together with {{tag}}?" -**Domain**: Tag Analytics -**Complexity**: Complex - -**SQL Template**: -```sql -WITH tag_combinations AS ( - SELECT - SUBSTRING_INDEX(SUBSTRING_INDEX(Tags, '><', n.n), '>', -1) AS tag_name - FROM codebase_community_template.posts - CROSS JOIN ( - SELECT 1 AS n UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL - SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL - SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 - ) n - WHERE PostTypeId = 1 - AND Tags LIKE '%<{{tag}}>%' - AND Tags LIKE '<%>' - AND n.n <= LENGTH(Tags) - LENGTH(REPLACE(Tags, '><', '')) + 1 - AND SUBSTRING_INDEX(SUBSTRING_INDEX(Tags, '><', n.n), '>', -1) != '{{tag}}' -) -SELECT - tag_name, - COUNT(*) AS co_occurrence_count -FROM tag_combinations -WHERE tag_name IS NOT NULL -GROUP BY tag_name -ORDER BY co_occurrence_count DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -WITH tag_combinations AS ( - SELECT - SUBSTRING_INDEX(SUBSTRING_INDEX(Tags, '><', n.n), '>', -1) AS tag_name - FROM codebase_community_template.posts - CROSS JOIN ( - SELECT 1 AS n UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL - SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL - SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 - ) n - WHERE PostTypeId = 1 - AND Tags LIKE '%%' - AND Tags LIKE '<%>' - AND n.n <= LENGTH(Tags) - LENGTH(REPLACE(Tags, '><', '')) + 1 - AND SUBSTRING_INDEX(SUBSTRING_INDEX(Tags, '><', n.n), '>', -1) != 'python' -) -SELECT tag_name, COUNT(*) AS co_occurrence_count -FROM tag_combinations -WHERE tag_name IS NOT NULL -GROUP BY tag_name -ORDER BY co_occurrence_count DESC -LIMIT 15; -``` - ---- - -### Template 37: Tag Difficulty -**Natural Language**: "What is the average answer count for questions tagged with {{tag}}?" -**Domain**: Tag Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - ROUND(AVG(AnswerCount), 2) AS avg_answers, - MIN(AnswerCount) AS min_answers, - MAX(AnswerCount) AS max_answers, - COUNT(*) AS total_questions, - SUM(CASE WHEN AnswerCount = 0 THEN 1 ELSE 0 END) AS unanswered_count -FROM codebase_community_template.posts -WHERE PostTypeId = 1 - AND Tags LIKE '%<{{tag}}>%'; -``` - -**Example**: -```sql -SELECT ROUND(AVG(AnswerCount), 2) AS avg_answers, - MIN(AnswerCount) AS min_answers, MAX(AnswerCount) AS max_answers, - COUNT(*) AS total_questions, - SUM(CASE WHEN AnswerCount = 0 THEN 1 ELSE 0 END) AS unanswered_count -FROM codebase_community_template.posts -WHERE PostTypeId = 1 AND Tags LIKE '%%'; -``` - ---- - -### Template 38: New Tags -**Natural Language**: "What are the newest tags created?" -**Domain**: Tag Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - t.TagName, - t.Count AS usage_count, - MIN(p.CreaionDate) AS first_used, - MAX(p.CreaionDate) AS last_used -FROM codebase_community_template.tags t -INNER JOIN codebase_community_template.posts p ON p.Tags LIKE CONCAT('%<', t.TagName, '>%') -WHERE p.PostTypeId = 1 -GROUP BY t.TagName, t.Count -HAVING first_used >= DATE_SUB(CURDATE(), INTERVAL {{days}} DAY) -ORDER BY first_used DESC -LIMIT {{N}}; -``` - -**Example**: -```sql -SELECT t.TagName, t.Count AS usage_count, - MIN(p.CreaionDate) AS first_used, - MAX(p.CreaionDate) AS last_used -FROM codebase_community_template.tags t -INNER JOIN codebase_community_template.posts p ON p.Tags LIKE CONCAT('%<', t.TagName, '>%') -WHERE p.PostTypeId = 1 -GROUP BY t.TagName, t.Count -HAVING first_used >= DATE_SUB(CURDATE(), INTERVAL 90 DAY) -ORDER BY first_used DESC -LIMIT 20; -``` - ---- - -### Template 39: Tag Wiki Information -**Natural Language**: "What is the wiki information for tag {{tag}}?" -**Domain**: Tag Analytics -**Complexity**: Medium - -**SQL Template**: -```sql -SELECT - t.TagName, - t.Count AS usage_count, - t.ExcerptPostId, - t.WikiPostId, - e.Title AS excerpt_title, - e.Body AS excerpt_body, - w.Title AS wiki_title, - w.Body AS wiki_body -FROM codebase_community_template.tags t -LEFT JOIN codebase_community_template.posts e ON t.ExcerptPostId = e.Id -LEFT JOIN codebase_community_template.posts w ON t.WikiPostId = w.Id -WHERE t.TagName = '{{tag}}'; -``` - -**Example**: -```sql -SELECT t.TagName, t.Count AS usage_count, t.ExcerptPostId, t.WikiPostId, - e.Title AS excerpt_title, e.Body AS excerpt_body, - w.Title AS wiki_title, w.Body AS wiki_body -FROM codebase_community_template.tags t -LEFT JOIN codebase_community_template.posts e ON t.ExcerptPostId = e.Id -LEFT JOIN codebase_community_template.posts w ON t.WikiPostId = w.Id -WHERE t.TagName = 'bayesian'; -``` - ---- - -### Template 40: Tag Network Analysis -**Natural Language**: "What is the question overlap between {{tag1}} and {{tag2}}?" -**Domain**: Tag Analytics -**Complexity**: Simple - -**SQL Template**: -```sql -SELECT - COUNT(*) AS questions_with_both_tags, - ROUND(COUNT(*) * 100.0 / ( - SELECT COUNT(*) FROM codebase_community_template.posts - WHERE PostTypeId = 1 AND (Tags LIKE '%<{{tag1}}>%' OR Tags LIKE '%<{{tag2}}>%') - ), 2) AS overlap_percentage -FROM codebase_community_template.posts -WHERE PostTypeId = 1 - AND Tags LIKE '%<{{tag1}}>%' - AND Tags LIKE '%<{{tag2}}>%'; -``` - -**Example**: -```sql -SELECT COUNT(*) AS questions_with_both_tags, - ROUND(COUNT(*) * 100.0 / ( - SELECT COUNT(*) FROM codebase_community_template.posts - WHERE PostTypeId = 1 AND (Tags LIKE '%%' OR Tags LIKE '%%') - ), 2) AS overlap_percentage -FROM codebase_community_template.posts -WHERE PostTypeId = 1 - AND Tags LIKE '%%' - AND Tags LIKE '%%'; -``` - ---- - -## Summary - -This document provides 40 comprehensive question templates covering: -- **10 User Analytics templates**: User reputation, activity, badges, voting behavior -- **10 Content Analytics templates**: Questions, answers, views, edits, quality -- **10 Engagement Analytics templates**: Comments, votes, interaction patterns -- **10 Tag Analytics templates**: Tag popularity, expertise, trends, relationships - -Each template is production-ready with natural language mappings, parameterized SQL, and concrete examples.