Skip to content

Add an ORDER BY example that would be used in a typical GROUP BY query. #395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 13, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 38 additions & 17 deletions source/includes/table-sql-to-agg-examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,39 @@ mongo3: |
total: { $sum: "$price" } } }
] )
desc4: |
For each unique ``cust_id``,
sum the ``price`` field,
results sorted by sum.
sql4: |
.. code-block:: sql

SELECT cust_id,
SUM(price) AS total
FROM orders
GROUP BY cust_id
ORDER BY total
mongo4: |
.. code-block:: javascript
:emphasize-lines: 2-4

db.orders.aggregate( [
{ $group: { _id: "$cust_id",
total: { $sum: "$price" } } },
{ $sort: { total: 1 } }
] )
desc5: |
For each unique
``cust_id``, ``ord_date`` grouping,
sum the ``price`` field.
sql4: |
sql5: |
.. code-block:: sql

SELECT cust_id,
ord_date,
SUM(price) AS total
FROM orders
GROUP BY cust_id, ord_date
mongo4: |
mongo5: |
.. code-block:: javascript
:emphasize-lines: 2-4

Expand All @@ -91,18 +112,18 @@ mongo4: |
ord_date: "$ord_date" },
total: { $sum: "$price" } } }
] )
desc5: |
desc6: |
For ``cust_id`` with multiple records,
return the ``cust_id`` and
the corresponding record count.
sql5: |
sql6: |
.. code-block:: sql

SELECT cust_id, count(*)
FROM orders
GROUP BY cust_id
HAVING count(*) > 1
mongo5: |
mongo6: |
.. code-block:: javascript
:emphasize-lines: 2-4

Expand All @@ -111,12 +132,12 @@ mongo5: |
count: { $sum: 1 } } },
{ $match: { count: { $gt: 1 } } }
] )
desc6: |
desc7: |
For each unique ``cust_id``, ``ord_date``
grouping, sum the ``price`` field
and return only where the
sum is greater than 250.
sql6: |
sql7: |
.. code-block:: sql

SELECT cust_id,
Expand All @@ -125,7 +146,7 @@ sql6: |
FROM orders
GROUP BY cust_id, ord_date
HAVING total > 250
mongo6: |
mongo7: |
.. code-block:: javascript
:emphasize-lines: 2-5

Expand All @@ -135,19 +156,19 @@ mongo6: |
total: { $sum: "$price" } } },
{ $match: { total: { $gt: 250 } } }
] )
desc7: |
desc8: |
For each unique ``cust_id``
with status ``A``,
sum the ``price`` field.
sql7: |
sql8: |
.. code-block:: sql

SELECT cust_id,
SUM(price) as total
FROM orders
WHERE status = 'A'
GROUP BY cust_id
mongo7: |
mongo8: |
.. code-block:: javascript
:emphasize-lines: 2-4

Expand All @@ -156,13 +177,13 @@ mongo7: |
{ $group: { _id: "$cust_id",
total: { $sum: "$price" } } }
] )
desc8: |
desc9: |
For each unique ``cust_id``
with status ``A``,
sum the ``price`` field and return
only where the
sum is greater than 250.
sql8: |
sql9: |
.. code-block:: sql

SELECT cust_id,
Expand All @@ -171,7 +192,7 @@ sql8: |
WHERE status = 'A'
GROUP BY cust_id
HAVING total > 250
mongo8: |
mongo9: |
.. code-block:: javascript
:emphasize-lines: 2-5

Expand All @@ -181,13 +202,13 @@ mongo8: |
total: { $sum: "$price" } } },
{ $match: { total: { $gt: 250 } } }
] )
desc9: |
desc10: |
For each unique ``cust_id``,
sum the corresponding
line item ``qty`` fields
associated with the
orders.
sql9: |
sql10: |
.. code-block:: sql

SELECT cust_id,
Expand All @@ -196,7 +217,7 @@ sql9: |
order_lineitem li
WHERE li.order_id = o.id
GROUP BY cust_id
mongo9: |
mongo10: |
.. code-block:: javascript
:emphasize-lines: 2-5

Expand Down