11# table structure. all content symbolic.
22section : layout
3- header : [ meta.header1, meta.header2 ]
3+ header : [ meta.header1, meta.header2, meta.header3 ]
44rows :
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 ]
1414---
1515# table metadata, as meta.<key>
1616section : meta
17- header1 : " SQL Example"
18- header2 : " MongoDB Example"
17+ header1 : " Description"
18+ header2 : " SQL Example"
19+ header3 : " MongoDB Example"
1920---
2021# table content, as content.<key>
2122section : content
23+ desc1 : |
24+ Count all records
25+ from ``orders``
2226sql1 : |
2327 .. code-block:: sql
2428
2529 SELECT COUNT(*) AS count
2630 FROM orders
2731mongo1 : |
2832 .. code-block:: javascript
29- :emphasize-lines: 2
33+ :emphasize-lines: 2-3
3034
3135 db.orders.aggregate( [
32- { $group: { _id: null, count: { $sum: 1 } } }
36+ { $group: { _id: null,
37+ count: { $sum: 1 } } }
3338 ] )
39+ desc2 : |
40+ Sum the ``price`` field
41+ from ``orders``
3442sql2 : |
3543 .. code-block:: sql
3644
3745 SELECT SUM(price) AS total
3846 FROM orders
3947mongo2 : |
4048 .. code-block:: javascript
41- :emphasize-lines: 2
49+ :emphasize-lines: 2-3
4250
4351 db.orders.aggregate( [
44- { $group: { _id: null, total: { $sum: "$price" } } }
52+ { $group: { _id: null,
53+ total: { $sum: "$price" } } }
4554 ] )
55+ desc3 : |
56+ For each unique ``cust_id``,
57+ sum the ``price`` field.
4658sql3 : |
4759 .. code-block:: sql
4860
49- SELECT cust_id, SUM(price) AS total
50- FROM orders
61+ SELECT cust_id,
62+ SUM(price) AS total
63+ FROM orders
5164 GROUP BY cust_id
5265mongo3 : |
53-
5466 .. code-block:: javascript
55- :emphasize-lines: 2
67+ :emphasize-lines: 2-3
5668
5769 db.orders.aggregate( [
58- { $group: { _id: "$cust_id", total: { $sum: "$price" } } }
70+ { $group: { _id: "$cust_id",
71+ total: { $sum: "$price" } } }
5972 ] )
73+ desc4 : |
74+ For each unique
75+ ``cust_id``, ``ord_date`` grouping,
76+ sum the ``price`` field.
6077sql4 : |
6178 .. code-block:: sql
6279
63- SELECT cust_id, ord_date, SUM(price) AS total
80+ SELECT cust_id,
81+ ord_date,
82+ SUM(price) AS total
6483 FROM orders
6584 GROUP BY cust_id, ord_date
6685mongo4 : |
6786 .. code-block:: javascript
68- :emphasize-lines: 2-3
87+ :emphasize-lines: 2-4
6988
7089 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" },
7292 total: { $sum: "$price" } } }
7393 ] )
94+ desc5 : |
95+ For ``cust_id`` with multiple records,
96+ return the ``cust_id`` and
97+ the corresponding record count.
7498sql5 : |
7599 .. code-block:: sql
76100
@@ -80,73 +104,105 @@ sql5: |
80104 HAVING count(*) > 1
81105mongo5 : |
82106 .. code-block:: javascript
83- :emphasize-lines: 2-3
84-
107+ :emphasize-lines: 2-4
108+
85109 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 } } }
88113 ] )
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.
89119sql6 : |
90120 .. code-block:: sql
91121
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
94126 GROUP BY cust_id, ord_date
95127 HAVING total > 250
96128mongo6 : |
97129 .. code-block:: javascript
98- :emphasize-lines: 2-4
130+ :emphasize-lines: 2-5
99131
100132 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" },
102135 total: { $sum: "$price" } } },
103136 { $match: { total: { $gt: 250 } } }
104137 ] )
138+ desc7 : |
139+ For each unique ``cust_id``
140+ with status ``A``,
141+ sum the ``price`` field.
105142sql7 : |
106143 .. code-block:: sql
107144
108- SELECT cust_id, SUM(price) as total
109- FROM orders
145+ SELECT cust_id,
146+ SUM(price) as total
147+ FROM orders
110148 WHERE status = 'A'
111149 GROUP BY cust_id
112150mongo7 : |
113151 .. code-block:: javascript
114- :emphasize-lines: 2-3
115-
152+ :emphasize-lines: 2-4
153+
116154 db.orders.aggregate( [
117155 { $match: { status: 'A' } },
118- { $group: { _id: "$cust_id", total: { $sum: "$price" } } }
156+ { $group: { _id: "$cust_id",
157+ total: { $sum: "$price" } } }
119158 ] )
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.
120165sql8 : |
121166 .. code-block:: sql
122167
123- SELECT cust_id, SUM(price) as total
124- FROM orders
168+ SELECT cust_id,
169+ SUM(price) as total
170+ FROM orders
125171 WHERE status = 'A'
126172 GROUP BY cust_id
127173 HAVING total > 250
128174mongo8 : |
129175 .. code-block:: javascript
130176 :emphasize-lines: 2-5
131-
177+
132178 db.orders.aggregate( [
133179 { $match: { status: 'A' } },
134- { $group: { _id: "$cust_id", total: { $sum: "$price" } } } ,
180+ { $group: { _id: "$cust_id",
181+ total: { $sum: "$price" } } },
135182 { $match: { total: { $gt: 250 } } }
136183 ] )
184+ desc9 : |
185+ For each unique ``cust_id``,
186+ sum the corresponding
187+ line item ``qty`` fields
188+ associated with the
189+ orders.
137190sql9 : |
138191 .. code-block:: sql
139192
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
143198 GROUP BY cust_id
144199mongo9 : |
145200 .. code-block:: javascript
146201 :emphasize-lines: 2-5
147-
202+
148203 db.orders.aggregate( [
149204 { $unwind: "$items" },
150- { $group: { _id: "$cust_id", qty: { $sum: "$items.qty" } } }
205+ { $group: { _id: "$cust_id",
206+ qty: { $sum: "$items.qty" } } }
151207 ] )
152208 ...
0 commit comments