Couchbase aggregation query
Couchbase is a distributed NoSQL database that supports multiple aggregate query operations, such as:
1. COUNT: Used to calculate the number of documents under specified conditions.
Example query:
SELECT COUNT(*) AS num_customers FROM `customers`
2. SUM: Used to calculate the sum of specified fields.
Example query:
SELECT SUM(price) AS total_price FROM `orders`
3. AVG: Used to calculate the average value of a specified field.
Example query:
SELECT AVG(rating) AS avg_rating FROM `products`
4. MIN: Used to find the minimum value of the specified field.
Example query:
SELECT MIN(price) AS min_price FROM `products`
5. MAX: Used to find the maximum value of the specified field.
Example query:
SELECT MAX(price) AS max_price FROM `products`
6. GROUP BY: Used to group results according to specified fields.
Example query:
SELECT category, AVG(price) AS avg_price FROM `products` GROUP BY category
7. HAVING: Used to filter the results after GROUP BY.
Example query:
SELECT category, AVG(price) AS avg_price FROM `products`
GROUP BY category HAVING AVG(price) > 100
It should be noted that before using Couchbase for aggregate queries, corresponding indexes need to be created to improve query performance. In the above example, 'customers',' orders', and 'products' respectively represent table names, while' price ',' rating ', and' category 'represent field names. The specific database table structure and sample data need to be set according to the actual situation.