| SELECT COUNT(*) AS count | | FROM orders |
| | db.orders.aggregate( [ | | { | | $group: { | | _id: null, | | count: { $sum: 1 } | | } | | } | | ] ) |
| |
| SELECT SUM(price) AS total | | FROM orders |
| | db.orders.aggregate( [ | | { | | $group: { | | _id: null, | | total: { $sum: "$price" } | | } | | } | | ] ) |
| |
| SELECT cust_id, | | SUM(price) AS total | | FROM orders | | GROUP BY cust_id |
| | db.orders.aggregate( [ | | { | | $group: { | | _id: "$cust_id", | | total: { $sum: "$price" } | | } | | } | | ] ) |
| 각 고유의 cust_id에 대해 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 } } | | ] ) |
| 각 고유의 cust_id에 대해 price 필드를 합산(결과는 합계 순으로 정렬) |
| 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" } | | } | | } | | ] ) |
| ord_date 그룹의 각 고유의 cust_id에 대해 price 필드를 합산, 날짜의 시간 부분은 제외
|
| 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 } } } | | ] ) |
| 복수의 기록이 있는 cust_id의 경우, cust_id와 해당 기록 수를 반환 |
| 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 } } } | | ] ) |
| ord_date 그룹의 각 고유의 cust_id에 대해 price 필드를 합산한 다음 합계가 250을 초과하는 경우에만 반환, 날짜의 시간 부분은 제외
|
| 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" } | | } | | } | | ] ) |
| 상태가 A인 각 고유의 cust_id에 대해 A 필드를 합산 |
| 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 } } } | | ] ) |
| 상태가 A인 각 고유의 cust_id에 대해 price 필드를 합산한 다음 합계가 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" } | | } | | } | | ] ) |
| 각 고유의 cust_id에 대해, 주문과 관련된 해당 항목 qty 필드를 합산 |
| 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 } | | } | | } | | ] ) |
| ord_date 그룹의 고유한 cust_id의 수를 계산, 날짜의 시간 부분은 제외
|