🐹 @group Operator in TuskLang - Go Guide

Go Documentation

@group Operator in TuskLang - Go Guide

πŸ“Š Group Power: @group Operator Unleashed

TuskLang's @group operator is your aggregation rebellion. We don't bow to any kingβ€”especially not to scattered, unorganized data. Here's how to use @group in Go projects to organize, aggregate, and analyze your data effectively.

πŸ“‹ Table of Contents

- What is @group? - Basic Usage - Grouping Strategies - Aggregation Functions - Go Integration - Best Practices

πŸ“Š What is @group?

The @group operator organizes data into groups and applies aggregation functions. No more scattered dataβ€”just pure, organized analysis.

πŸ› οΈ Basic Usage

[group_operations]
users_by_department: @group.by(@query("SELECT * FROM users"), "department")
orders_by_status: @group.by(@query("SELECT * FROM orders"), "status")
sales_by_month: @group.by(@query("SELECT * FROM sales"), "month")

πŸ”§ Grouping Strategies

Single Field Grouping

[single_grouping]
by_department: @group.by(@query("SELECT * FROM employees"), "department")
by_status: @group.by(@query("SELECT * FROM orders"), "status")
by_region: @group.by(@query("SELECT * FROM customers"), "region")

Multiple Field Grouping

[multi_grouping]
by_dept_and_role: @group.by(@query("SELECT * FROM employees"), ["department", "role"])
by_status_and_date: @group.by(@query("SELECT * FROM orders"), ["status", "date"])
by_region_and_type: @group.by(@query("SELECT * FROM customers"), ["region", "type"])

Conditional Grouping

[conditional_grouping]
active_by_dept: @group.by_condition(@query("SELECT * FROM users"), "department", "status = 'active'")
high_value_by_status: @group.by_condition(@query("SELECT * FROM orders"), "status", "amount > 1000")
recent_by_category: @group.by_condition(@query("SELECT * FROM events"), "category", "created_at > NOW() - INTERVAL '1 day'")

πŸ“ˆ Aggregation Functions

Count Aggregations

[counts]
user_count_by_dept: @group.count(@query("SELECT * FROM users"), "department")
order_count_by_status: @group.count(@query("SELECT * FROM orders"), "status")
product_count_by_category: @group.count(@query("SELECT * FROM products"), "category")

Sum Aggregations

[sums]
total_sales_by_dept: @group.sum(@query("SELECT * FROM sales"), "department", "amount")
total_orders_by_status: @group.sum(@query("SELECT * FROM orders"), "status", "quantity")
total_revenue_by_month: @group.sum(@query("SELECT * FROM revenue"), "month", "amount")

Average Aggregations

[averages]
avg_salary_by_dept: @group.avg(@query("SELECT * FROM employees"), "department", "salary")
avg_order_value: @group.avg(@query("SELECT * FROM orders"), "status", "amount")
avg_rating_by_product: @group.avg(@query("SELECT * FROM reviews"), "product_id", "rating")

Min/Max Aggregations

[min_max]
min_salary_by_dept: @group.min(@query("SELECT * FROM employees"), "department", "salary")
max_order_by_status: @group.max(@query("SELECT * FROM orders"), "status", "amount")
min_price_by_category: @group.min(@query("SELECT * FROM products"), "category", "price")
max_rating_by_product: @group.max(@query("SELECT * FROM reviews"), "product_id", "rating")

Custom Aggregations

[custom]
unique_users_by_dept: @group.unique(@query("SELECT * FROM users"), "department", "user_id")
top_products_by_category: @group.top(@query("SELECT * FROM products"), "category", "sales", 5)
bottom_products_by_category: @group.bottom(@query("SELECT * FROM products"), "category", "sales", 5)

πŸ”— Go Integration

// Access grouped data
usersByDept := config.GetObject("users_by_department")
ordersByStatus := config.GetObject("orders_by_status")
salesByMonth := config.GetObject("sales_by_month")

// Process grouped results for dept, users := range usersByDept { fmt.Printf("Department %s: %d users\n", dept, len(users.([]interface{}))) }

Manual Group Implementation

type DataGrouper struct{}

func (g *DataGrouper) GroupBy(data []map[string]interface{}, field string) map[string][]map[string]interface{} { result := make(map[string][]map[string]interface{}) for _, item := range data { if value, exists := item[field]; exists { key := fmt.Sprint(value) result[key] = append(result[key], item) } } return result }

func (g *DataGrouper) GroupByMultiple(data []map[string]interface{}, fields []string) map[string][]map[string]interface{} { result := make(map[string][]map[string]interface{}) for _, item := range data { var keyParts []string for _, field := range fields { if value, exists := item[field]; exists { keyParts = append(keyParts, fmt.Sprint(value)) } } key := strings.Join(keyParts, "_") result[key] = append(result[key], item) } return result }

func (g *DataGrouper) Count(data []map[string]interface{}, groupField string) map[string]int { result := make(map[string]int) for _, item := range data { if value, exists := item[groupField]; exists { key := fmt.Sprint(value) result[key]++ } } return result }

func (g *DataGrouper) Sum(data []map[string]interface{}, groupField, sumField string) map[string]float64 { result := make(map[string]float64) for _, item := range data { if groupValue, exists := item[groupField]; exists { if sumValue, exists := item[sumField]; exists { key := fmt.Sprint(groupValue) if num, ok := sumValue.(float64); ok { result[key] += num } } } } return result }

πŸ₯‡ Best Practices

- Use meaningful group names - Consider performance with large datasets - Validate group field existence - Use appropriate aggregation functions - Document grouping logic clearly

---

TuskLang: Organized analysis with @group.