| SELECT COUNT(*) AS count | | FROM orders |
| | db.orders.aggregate( [ | | { | | $group: { | | _id: null, | | count: { $sum: 1 } | | } | | } | | ] ) |
| 次の場所からすべてのレコードをカウントする: orders |
| SELECT SUM(price) AS total | | FROM orders |
| | db.orders.aggregate( [ | | { | | $group: { | | _id: null, | | total: { $sum: "$price" } | | } | | } | | ] ) |
| orders の 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" } | | } | | } | | ] ) |
| それぞれの一意の cust_id、ord_date グループについて、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 } } } | | ] ) |
| 一意の cust_id、ord_date グループごとに、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 ごとに、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 } } } | | ] ) |
| ステータスが 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 } | | } | | } | | ] ) |
| 異なる cust_id、ord_date グループの数をカウントします。日付の時刻部分を除外します。 |