Skip to content

Commit 85273fc

Browse files
committed
merge: @mkwan changes to aggregation examples table
2 parents ed89905 + 99eec2a commit 85273fc

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

source/includes/table-sql-to-agg-examples.yaml

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,39 @@ mongo3: |
7171
total: { $sum: "$price" } } }
7272
] )
7373
desc4: |
74+
For each unique ``cust_id``,
75+
sum the ``price`` field,
76+
results sorted by sum.
77+
sql4: |
78+
.. code-block:: sql
79+
80+
SELECT cust_id,
81+
SUM(price) AS total
82+
FROM orders
83+
GROUP BY cust_id
84+
ORDER BY total
85+
mongo4: |
86+
.. code-block:: javascript
87+
:emphasize-lines: 2-4
88+
89+
db.orders.aggregate( [
90+
{ $group: { _id: "$cust_id",
91+
total: { $sum: "$price" } } },
92+
{ $sort: { total: 1 } }
93+
] )
94+
desc5: |
7495
For each unique
7596
``cust_id``, ``ord_date`` grouping,
7697
sum the ``price`` field.
77-
sql4: |
98+
sql5: |
7899
.. code-block:: sql
79100
80101
SELECT cust_id,
81102
ord_date,
82103
SUM(price) AS total
83104
FROM orders
84105
GROUP BY cust_id, ord_date
85-
mongo4: |
106+
mongo5: |
86107
.. code-block:: javascript
87108
:emphasize-lines: 2-4
88109
@@ -91,18 +112,18 @@ mongo4: |
91112
ord_date: "$ord_date" },
92113
total: { $sum: "$price" } } }
93114
] )
94-
desc5: |
115+
desc6: |
95116
For ``cust_id`` with multiple records,
96117
return the ``cust_id`` and
97118
the corresponding record count.
98-
sql5: |
119+
sql6: |
99120
.. code-block:: sql
100121
101122
SELECT cust_id, count(*)
102123
FROM orders
103124
GROUP BY cust_id
104125
HAVING count(*) > 1
105-
mongo5: |
126+
mongo6: |
106127
.. code-block:: javascript
107128
:emphasize-lines: 2-4
108129
@@ -111,12 +132,12 @@ mongo5: |
111132
count: { $sum: 1 } } },
112133
{ $match: { count: { $gt: 1 } } }
113134
] )
114-
desc6: |
135+
desc7: |
115136
For each unique ``cust_id``, ``ord_date``
116137
grouping, sum the ``price`` field
117138
and return only where the
118139
sum is greater than 250.
119-
sql6: |
140+
sql7: |
120141
.. code-block:: sql
121142
122143
SELECT cust_id,
@@ -125,7 +146,7 @@ sql6: |
125146
FROM orders
126147
GROUP BY cust_id, ord_date
127148
HAVING total > 250
128-
mongo6: |
149+
mongo7: |
129150
.. code-block:: javascript
130151
:emphasize-lines: 2-5
131152
@@ -135,19 +156,19 @@ mongo6: |
135156
total: { $sum: "$price" } } },
136157
{ $match: { total: { $gt: 250 } } }
137158
] )
138-
desc7: |
159+
desc8: |
139160
For each unique ``cust_id``
140161
with status ``A``,
141162
sum the ``price`` field.
142-
sql7: |
163+
sql8: |
143164
.. code-block:: sql
144165
145166
SELECT cust_id,
146167
SUM(price) as total
147168
FROM orders
148169
WHERE status = 'A'
149170
GROUP BY cust_id
150-
mongo7: |
171+
mongo8: |
151172
.. code-block:: javascript
152173
:emphasize-lines: 2-4
153174
@@ -156,13 +177,13 @@ mongo7: |
156177
{ $group: { _id: "$cust_id",
157178
total: { $sum: "$price" } } }
158179
] )
159-
desc8: |
180+
desc9: |
160181
For each unique ``cust_id``
161182
with status ``A``,
162183
sum the ``price`` field and return
163184
only where the
164185
sum is greater than 250.
165-
sql8: |
186+
sql9: |
166187
.. code-block:: sql
167188
168189
SELECT cust_id,
@@ -171,7 +192,7 @@ sql8: |
171192
WHERE status = 'A'
172193
GROUP BY cust_id
173194
HAVING total > 250
174-
mongo8: |
195+
mongo9: |
175196
.. code-block:: javascript
176197
:emphasize-lines: 2-5
177198
@@ -181,13 +202,13 @@ mongo8: |
181202
total: { $sum: "$price" } } },
182203
{ $match: { total: { $gt: 250 } } }
183204
] )
184-
desc9: |
205+
desc10: |
185206
For each unique ``cust_id``,
186207
sum the corresponding
187208
line item ``qty`` fields
188209
associated with the
189210
orders.
190-
sql9: |
211+
sql10: |
191212
.. code-block:: sql
192213
193214
SELECT cust_id,
@@ -196,7 +217,7 @@ sql9: |
196217
order_lineitem li
197218
WHERE li.order_id = o.id
198219
GROUP BY cust_id
199-
mongo9: |
220+
mongo10: |
200221
.. code-block:: javascript
201222
:emphasize-lines: 2-5
202223

0 commit comments

Comments
 (0)