1
1
# table structure. all content symbolic.
2
2
section : layout
3
- header : [ meta.header1, meta.header2 ]
3
+ header : [ meta.header1, meta.header2, meta.header3 ]
4
4
rows :
5
- - 1 : [ content.sql1, content.mongo1 ]
6
- - 2 : [ content.sql2, content.mongo2 ]
7
- - 3 : [ content.sql3, content.mongo3 ]
8
- - 4 : [ content.sql4, content.mongo4 ]
9
- - 5 : [ content.sql5, content.mongo5 ]
10
- - 6 : [ content.sql6, content.mongo6 ]
11
- - 7 : [ content.sql7, content.mongo7 ]
12
- - 8 : [ content.sql8, content.mongo8 ]
13
- - 9 : [ content.sql9, content.mongo9 ]
5
+ - 1 : [ content.desc1, content. sql1, content.mongo1 ]
6
+ - 2 : [ content.desc2, content. sql2, content.mongo2 ]
7
+ - 3 : [ content.desc3, content. sql3, content.mongo3 ]
8
+ - 4 : [ content.desc4, content. sql4, content.mongo4 ]
9
+ - 5 : [ content.desc5, content. sql5, content.mongo5 ]
10
+ - 6 : [ content.desc6, content. sql6, content.mongo6 ]
11
+ - 7 : [ content.desc7, content. sql7, content.mongo7 ]
12
+ - 8 : [ content.desc8, content. sql8, content.mongo8 ]
13
+ - 9 : [ content.desc9, content. sql9, content.mongo9 ]
14
14
---
15
15
# table metadata, as meta.<key>
16
16
section : meta
17
- header1 : " SQL Example"
18
- header2 : " MongoDB Example"
17
+ header1 : " Description"
18
+ header2 : " SQL Example"
19
+ header3 : " MongoDB Example"
19
20
---
20
21
# table content, as content.<key>
21
22
section : content
23
+ desc1 : |
24
+ Count all records
25
+ from ``orders``
22
26
sql1 : |
23
27
.. code-block:: sql
24
28
25
29
SELECT COUNT(*) AS count
26
30
FROM orders
27
31
mongo1 : |
28
32
.. code-block:: javascript
29
- :emphasize-lines: 2
33
+ :emphasize-lines: 2-3
30
34
31
35
db.orders.aggregate( [
32
- { $group: { _id: null, count: { $sum: 1 } } }
36
+ { $group: { _id: null,
37
+ count: { $sum: 1 } } }
33
38
] )
39
+ desc2 : |
40
+ Sum the ``price`` field
41
+ from ``orders``
34
42
sql2 : |
35
43
.. code-block:: sql
36
44
37
45
SELECT SUM(price) AS total
38
46
FROM orders
39
47
mongo2 : |
40
48
.. code-block:: javascript
41
- :emphasize-lines: 2
49
+ :emphasize-lines: 2-3
42
50
43
51
db.orders.aggregate( [
44
- { $group: { _id: null, total: { $sum: "$price" } } }
52
+ { $group: { _id: null,
53
+ total: { $sum: "$price" } } }
45
54
] )
55
+ desc3 : |
56
+ For each unique ``cust_id``,
57
+ sum the ``price`` field.
46
58
sql3 : |
47
59
.. code-block:: sql
48
60
49
- SELECT cust_id, SUM(price) AS total
50
- FROM orders
61
+ SELECT cust_id,
62
+ SUM(price) AS total
63
+ FROM orders
51
64
GROUP BY cust_id
52
65
mongo3 : |
53
-
54
66
.. code-block:: javascript
55
- :emphasize-lines: 2
67
+ :emphasize-lines: 2-3
56
68
57
69
db.orders.aggregate( [
58
- { $group: { _id: "$cust_id", total: { $sum: "$price" } } }
70
+ { $group: { _id: "$cust_id",
71
+ total: { $sum: "$price" } } }
59
72
] )
73
+ desc4 : |
74
+ For each unique
75
+ ``cust_id``, ``ord_date`` grouping,
76
+ sum the ``price`` field.
60
77
sql4 : |
61
78
.. code-block:: sql
62
79
63
- SELECT cust_id, ord_date, SUM(price) AS total
80
+ SELECT cust_id,
81
+ ord_date,
82
+ SUM(price) AS total
64
83
FROM orders
65
84
GROUP BY cust_id, ord_date
66
85
mongo4 : |
67
86
.. code-block:: javascript
68
- :emphasize-lines: 2-3
87
+ :emphasize-lines: 2-4
69
88
70
89
db.orders.aggregate( [
71
- { $group: { _id: { cust_id: "$cust_id", ord_date: "$ord_date" },
90
+ { $group: { _id: { cust_id: "$cust_id",
91
+ ord_date: "$ord_date" },
72
92
total: { $sum: "$price" } } }
73
93
] )
94
+ desc5 : |
95
+ For ``cust_id`` with multiple records,
96
+ return the ``cust_id`` and
97
+ the corresponding record count.
74
98
sql5 : |
75
99
.. code-block:: sql
76
100
@@ -80,73 +104,105 @@ sql5: |
80
104
HAVING count(*) > 1
81
105
mongo5 : |
82
106
.. code-block:: javascript
83
- :emphasize-lines: 2-3
84
-
107
+ :emphasize-lines: 2-4
108
+
85
109
db.orders.aggregate( [
86
- { $group: { _id: "$cust_id", count: { $sum: 1 } } },
87
- { $match: { count: { $gt: 1 } } }
110
+ { $group: { _id: "$cust_id",
111
+ count: { $sum: 1 } } },
112
+ { $match: { count: { $gt: 1 } } }
88
113
] )
114
+ desc6 : |
115
+ For each unique ``cust_id``, ``ord_date``
116
+ grouping, sum the ``price`` field
117
+ and return only where the
118
+ sum is greater than 250.
89
119
sql6 : |
90
120
.. code-block:: sql
91
121
92
- SELECT cust_id, ord_date, SUM(price) AS total
93
- FROM orders
122
+ SELECT cust_id,
123
+ ord_date,
124
+ SUM(price) AS total
125
+ FROM orders
94
126
GROUP BY cust_id, ord_date
95
127
HAVING total > 250
96
128
mongo6 : |
97
129
.. code-block:: javascript
98
- :emphasize-lines: 2-4
130
+ :emphasize-lines: 2-5
99
131
100
132
db.orders.aggregate( [
101
- { $group: { _id: { cust_id: "$cust_id", ord_date: "$ord_date" },
133
+ { $group: { _id: { cust_id: "$cust_id",
134
+ ord_date: "$ord_date" },
102
135
total: { $sum: "$price" } } },
103
136
{ $match: { total: { $gt: 250 } } }
104
137
] )
138
+ desc7 : |
139
+ For each unique ``cust_id``
140
+ with status ``A``,
141
+ sum the ``price`` field.
105
142
sql7 : |
106
143
.. code-block:: sql
107
144
108
- SELECT cust_id, SUM(price) as total
109
- FROM orders
145
+ SELECT cust_id,
146
+ SUM(price) as total
147
+ FROM orders
110
148
WHERE status = 'A'
111
149
GROUP BY cust_id
112
150
mongo7 : |
113
151
.. code-block:: javascript
114
- :emphasize-lines: 2-3
115
-
152
+ :emphasize-lines: 2-4
153
+
116
154
db.orders.aggregate( [
117
155
{ $match: { status: 'A' } },
118
- { $group: { _id: "$cust_id", total: { $sum: "$price" } } }
156
+ { $group: { _id: "$cust_id",
157
+ total: { $sum: "$price" } } }
119
158
] )
159
+ desc8 : |
160
+ For each unique ``cust_id``
161
+ with status ``A``,
162
+ sum the ``price`` field and return
163
+ only where the
164
+ sum is greater than 250.
120
165
sql8 : |
121
166
.. code-block:: sql
122
167
123
- SELECT cust_id, SUM(price) as total
124
- FROM orders
168
+ SELECT cust_id,
169
+ SUM(price) as total
170
+ FROM orders
125
171
WHERE status = 'A'
126
172
GROUP BY cust_id
127
173
HAVING total > 250
128
174
mongo8 : |
129
175
.. code-block:: javascript
130
176
:emphasize-lines: 2-5
131
-
177
+
132
178
db.orders.aggregate( [
133
179
{ $match: { status: 'A' } },
134
- { $group: { _id: "$cust_id", total: { $sum: "$price" } } } ,
180
+ { $group: { _id: "$cust_id",
181
+ total: { $sum: "$price" } } },
135
182
{ $match: { total: { $gt: 250 } } }
136
183
] )
184
+ desc9 : |
185
+ For each unique ``cust_id``,
186
+ sum the corresponding
187
+ line item ``qty`` fields
188
+ associated with the
189
+ orders.
137
190
sql9 : |
138
191
.. code-block:: sql
139
192
140
- SELECT cust_id,sum(li.qty) as qty
141
- FROM orders o, order_lineitem li
142
- WHERE li.order_id=o.id
193
+ SELECT cust_id,
194
+ SUM(li.qty) as qty
195
+ FROM orders o,
196
+ order_lineitem li
197
+ WHERE li.order_id = o.id
143
198
GROUP BY cust_id
144
199
mongo9 : |
145
200
.. code-block:: javascript
146
201
:emphasize-lines: 2-5
147
-
202
+
148
203
db.orders.aggregate( [
149
204
{ $unwind: "$items" },
150
- { $group: { _id: "$cust_id", qty: { $sum: "$items.qty" } } }
205
+ { $group: { _id: "$cust_id",
206
+ qty: { $sum: "$items.qty" } } }
151
207
] )
152
208
...
0 commit comments