| SELECT COUNT(*) AS count | | FROM orders |
| | db.orders.aggregate( [ | | { | | $group: { | | _id: null, | | count: { $sum: 1 } | | } | | } | | ] ) |
| Contagem de todos os registros de orders |
| SELECT SUM(price) AS total | | FROM orders |
| | db.orders.aggregate( [ | | { | | $group: { | | _id: null, | | total: { $sum: "$price" } | | } | | } | | ] ) |
| Soma o campo price de orders |
| SELECT cust_id, | | SUM(price) AS total | | FROM orders | | GROUP BY cust_id |
| | db.orders.aggregate( [ | | { | | $group: { | | _id: "$cust_id", | | total: { $sum: "$price" } | | } | | } | | ] ) |
| Para cada cust_id único, soma o campo price. |
| SELECT cust_id, | | SUM(price) AS total | | FROM orders | | GROUP BY cust_id | | ORDER BY total |
| | db.orders.aggregate( [ | | { | | $group: { | | _id: "$cust_id", | | total: { $sum: "$price" } | | } | | }, | | { $sort: { total: 1 } } | | ] ) |
| Para cada cust_id exclusivo, some o campo price , resultados classificados por soma. |
| SELECT cust_id, | | ord_date, | | SUM(price) AS total | | FROM orders | | GROUP 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 agrupamento exclusivo cust_id, ord_date , some o campo price . Exclui a parte de tempo da data. |
| SELECT cust_id, | | count(*) | | FROM orders | | GROUP BY cust_id | | HAVING count(*) > 1 |
| | db.orders.aggregate( [ | | { | | $group: { | | _id: "$cust_id", | | count: { $sum: 1 } | | } | | }, | | { $match: { count: { $gt: 1 } } } | | ] ) |
| Para cust_id com múltiplos registros, retorne o cust_id e a contagem de registro correspondente. |
| SELECT cust_id, | | ord_date, | | SUM(price) AS total | | FROM orders | | GROUP BY cust_id, | | ord_date | | HAVING 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 agrupamento exclusivo de cust_id, ord_date, some o campo price e retorne somente quando a soma for maior que 250. Exclui a parte de tempo da data. |
| SELECT cust_id, | | SUM(price) as total | | FROM orders | | WHERE status = 'A' | | GROUP BY cust_id |
| | db.orders.aggregate( [ | | { $match: { status: 'A' } }, | | { | | $group: { | | _id: "$cust_id", | | total: { $sum: "$price" } | | } | | } | | ] ) |
| Para cada cust_id único com status A, soma o campo price. |
| SELECT cust_id, | | SUM(price) as total | | FROM orders | | WHERE status = 'A' | | GROUP BY cust_id | | HAVING total > 250 |
| | db.orders.aggregate( [ | | { $match: { status: 'A' } }, | | { | | $group: { | | _id: "$cust_id", | | total: { $sum: "$price" } | | } | | }, | | { $match: { total: { $gt: 250 } } } | | ] ) |
| Para cada cust_id exclusivo com status A, some o campo price e retorne apenas quando a soma for maior que 250. |
| SELECT cust_id, | | SUM(li.qty) as qty | | FROM orders o, | | order_lineitem li | | WHERE li.order_id = o.id | | GROUP BY cust_id |
| | db.orders.aggregate( [ | | { $unwind: "$items" }, | | { | | $group: { | | _id: "$cust_id", | | qty: { $sum: "$items.qty" } | | } | | } | | ] ) |
| Para cada cust_id único, soma os campos de item de linha qty correspondentes associados aos 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 } | | } | | } | | ] ) |
| Conte o número de agrupamentos cust_id, ord_date distintos. Exclui a parte de tempo da data. |