@@ -71,18 +71,39 @@ mongo3: |
71
71
total: { $sum: "$price" } } }
72
72
] )
73
73
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 : |
74
95
For each unique
75
96
``cust_id``, ``ord_date`` grouping,
76
97
sum the ``price`` field.
77
- sql4 : |
98
+ sql5 : |
78
99
.. code-block:: sql
79
100
80
101
SELECT cust_id,
81
102
ord_date,
82
103
SUM(price) AS total
83
104
FROM orders
84
105
GROUP BY cust_id, ord_date
85
- mongo4 : |
106
+ mongo5 : |
86
107
.. code-block:: javascript
87
108
:emphasize-lines: 2-4
88
109
@@ -91,18 +112,18 @@ mongo4: |
91
112
ord_date: "$ord_date" },
92
113
total: { $sum: "$price" } } }
93
114
] )
94
- desc5 : |
115
+ desc6 : |
95
116
For ``cust_id`` with multiple records,
96
117
return the ``cust_id`` and
97
118
the corresponding record count.
98
- sql5 : |
119
+ sql6 : |
99
120
.. code-block:: sql
100
121
101
122
SELECT cust_id, count(*)
102
123
FROM orders
103
124
GROUP BY cust_id
104
125
HAVING count(*) > 1
105
- mongo5 : |
126
+ mongo6 : |
106
127
.. code-block:: javascript
107
128
:emphasize-lines: 2-4
108
129
@@ -111,12 +132,12 @@ mongo5: |
111
132
count: { $sum: 1 } } },
112
133
{ $match: { count: { $gt: 1 } } }
113
134
] )
114
- desc6 : |
135
+ desc7 : |
115
136
For each unique ``cust_id``, ``ord_date``
116
137
grouping, sum the ``price`` field
117
138
and return only where the
118
139
sum is greater than 250.
119
- sql6 : |
140
+ sql7 : |
120
141
.. code-block:: sql
121
142
122
143
SELECT cust_id,
@@ -125,7 +146,7 @@ sql6: |
125
146
FROM orders
126
147
GROUP BY cust_id, ord_date
127
148
HAVING total > 250
128
- mongo6 : |
149
+ mongo7 : |
129
150
.. code-block:: javascript
130
151
:emphasize-lines: 2-5
131
152
@@ -135,19 +156,19 @@ mongo6: |
135
156
total: { $sum: "$price" } } },
136
157
{ $match: { total: { $gt: 250 } } }
137
158
] )
138
- desc7 : |
159
+ desc8 : |
139
160
For each unique ``cust_id``
140
161
with status ``A``,
141
162
sum the ``price`` field.
142
- sql7 : |
163
+ sql8 : |
143
164
.. code-block:: sql
144
165
145
166
SELECT cust_id,
146
167
SUM(price) as total
147
168
FROM orders
148
169
WHERE status = 'A'
149
170
GROUP BY cust_id
150
- mongo7 : |
171
+ mongo8 : |
151
172
.. code-block:: javascript
152
173
:emphasize-lines: 2-4
153
174
@@ -156,13 +177,13 @@ mongo7: |
156
177
{ $group: { _id: "$cust_id",
157
178
total: { $sum: "$price" } } }
158
179
] )
159
- desc8 : |
180
+ desc9 : |
160
181
For each unique ``cust_id``
161
182
with status ``A``,
162
183
sum the ``price`` field and return
163
184
only where the
164
185
sum is greater than 250.
165
- sql8 : |
186
+ sql9 : |
166
187
.. code-block:: sql
167
188
168
189
SELECT cust_id,
@@ -171,7 +192,7 @@ sql8: |
171
192
WHERE status = 'A'
172
193
GROUP BY cust_id
173
194
HAVING total > 250
174
- mongo8 : |
195
+ mongo9 : |
175
196
.. code-block:: javascript
176
197
:emphasize-lines: 2-5
177
198
@@ -181,13 +202,13 @@ mongo8: |
181
202
total: { $sum: "$price" } } },
182
203
{ $match: { total: { $gt: 250 } } }
183
204
] )
184
- desc9 : |
205
+ desc10 : |
185
206
For each unique ``cust_id``,
186
207
sum the corresponding
187
208
line item ``qty`` fields
188
209
associated with the
189
210
orders.
190
- sql9 : |
211
+ sql10 : |
191
212
.. code-block:: sql
192
213
193
214
SELECT cust_id,
@@ -196,7 +217,7 @@ sql9: |
196
217
order_lineitem li
197
218
WHERE li.order_id = o.id
198
219
GROUP BY cust_id
199
- mongo9 : |
220
+ mongo10 : |
200
221
.. code-block:: javascript
201
222
:emphasize-lines: 2-5
202
223
0 commit comments