SELECT COUNT(*) AS countFROM orders
| db.orders.aggregate( [ { $group: { _id: null, count: { $sum: 1 } } }] )
| Contar todos los registros de orders |
SELECT SUM(price) AS totalFROM orders
| db.orders.aggregate( [ { $group: { _id: null, total: { $sum: "$price" } } }] )
| Sumar el campo price de orders |
SELECT cust_id, SUM(price) AS totalFROM ordersGROUP BY cust_id
| db.orders.aggregate( [ { $group: { _id: "$cust_id", total: { $sum: "$price" } } }] )
| Para cada cust_id único, sumar el campo price. |
SELECT cust_id, SUM(price) AS totalFROM ordersGROUP BY cust_idORDER BY total
| db.orders.aggregate( [ { $group: { _id: "$cust_id", total: { $sum: "$price" } } }, { $sort: { total: 1 } }] )
| Para cada cust_id único, suma el campo price, con resultados ordenados por suma. |
SELECT cust_id, ord_date, SUM(price) AS totalFROM ordersGROUP BY cust_id, ord_date
| db.orders.aggregate( [ { $group: { _id: { cust_id: "$cust_id", ord_date: { $dateToString: { format: "%Y-%m-%d", date: "$ord_date" }} }, total: { $sum: "$price" } } }] )
| Para cada agrupación única de cust_id, ord_date, suma el campo price. Excluye la porción de tiempo de la fecha. |
SELECT cust_id, count(*)FROM ordersGROUP BY cust_idHAVING count(*) > 1
| db.orders.aggregate( [ { $group: { _id: "$cust_id", count: { $sum: 1 } } }, { $match: { count: { $gt: 1 } } }] )
| Para cust_id con varios registros, devuelve el cust_id y el recuento de registros correspondiente. |
SELECT cust_id, ord_date, SUM(price) AS totalFROM ordersGROUP BY cust_id, ord_dateHAVING total > 250
| db.orders.aggregate( [ { $group: { _id: { cust_id: "$cust_id", ord_date: { $dateToString: { format: "%Y-%m-%d", date: "$ord_date" }} }, total: { $sum: "$price" } } }, { $match: { total: { $gt: 250 } } }] )
| Para cada agrupación única cust_id, ord_date, sume el campo price y devuelva solo cuando la suma sea mayor que 250. Excluye la parte de tiempo de la fecha. |
SELECT cust_id, SUM(price) as totalFROM ordersWHERE status = 'A'GROUP BY cust_id
| db.orders.aggregate( [ { $match: { status: 'A' } }, { $group: { _id: "$cust_id", total: { $sum: "$price" } } }] )
| Para cada cust_id único con estado A, sume el campo price. |
SELECT cust_id, SUM(price) as totalFROM ordersWHERE status = 'A'GROUP BY cust_idHAVING total > 250
| db.orders.aggregate( [ { $match: { status: 'A' } }, { $group: { _id: "$cust_id", total: { $sum: "$price" } } }, { $match: { total: { $gt: 250 } } }] )
| Para cada cust_id único con estado A, sume el campo price y devuelva solo donde la suma sea mayor que 250. |
SELECT cust_id, SUM(li.qty) as qtyFROM orders o, order_lineitem liWHERE li.order_id = o.idGROUP BY cust_id
| db.orders.aggregate( [ { $unwind: "$items" }, { $group: { _id: "$cust_id", qty: { $sum: "$items.qty" } } }] )
| Para cada cust_id único, sumar los campos de la línea de pedido qty correspondientes asociados a los pedidos. |
SELECT COUNT(*)FROM (SELECT cust_id, ord_date FROM orders GROUP BY cust_id, ord_date) as DerivedTable
| db.orders.aggregate( [ { $group: { _id: { cust_id: "$cust_id", ord_date: { $dateToString: { format: "%Y-%m-%d", date: "$ord_date" }} } } }, { $group: { _id: null, count: { $sum: 1 } } }] )
| Cuenta el número de agrupaciones distintas de cust_id, ord_date. Excluye la porción de tiempo de la fecha. |