You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 02_activities/assignments/Assignment2.md
+9Lines changed: 9 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -86,16 +86,20 @@ Find the NULLs and then using COALESCE, replace the NULL with a blank for the fi
86
86
87
87
**HINT**: keep the syntax the same, but edited the correct components with the string. The `||` values concatenate the columns into strings. Edit the appropriate columns -- you're making two edits -- and the NULL rows will be fixed. All the other rows will remain the same.
88
88
89
+
<divalign="center">-</div>
90
+
89
91
#### Windowed Functions
90
92
1. Write a query that selects from the customer_purchases table and numbers each customer’s visits to the farmer’s market (labeling each market date with a different number). Each customer’s first visit is labeled 1, second visit is labeled 2, etc.
91
93
92
94
You can either display all rows in the customer_purchases table, with the counter changing on each new market date for each customer, or select only the unique market dates per customer (without purchase details) and number those visits.
95
+
93
96
**HINT**: One of these approaches uses ROW_NUMBER() and one uses DENSE_RANK().
94
97
95
98
2. Reverse the numbering of the query from a part so each customer’s most recent visit is labeled 1, then write another query that uses this one as a subquery (or temp table) and filters the results to only the customer’s most recent visit.
96
99
97
100
3. Using a COUNT() window function, include a value along with each row of the customer_purchases table that indicates how many different times that customer has purchased that product_id.
98
101
102
+
<divalign="center">-</div>
99
103
100
104
#### String manipulations
101
105
1. Some product names in the product table have descriptions like "Jar" or "Organic". These are separated from the product name with a hyphen. Create a column using SUBSTR (and a couple of other commands) that captures these, but is otherwise NULL. Remove any trailing or leading whitespaces. Don't just use a case statement for each product!
@@ -106,6 +110,8 @@ You can either display all rows in the customer_purchases table, with the counte
106
110
107
111
**HINT**: you might need to use INSTR(product_name,'-') to find the hyphens. INSTR will help split the column.
108
112
113
+
<divalign="center">-</div>
114
+
109
115
#### UNION
110
116
1. Using a UNION, write a query that displays the market dates with the highest and lowest total sales.
111
117
@@ -129,18 +135,21 @@ Steps to complete this part of the assignment:
129
135
130
136
**HINT**: Be sure you select only relevant columns and rows. Remember, CROSS JOIN will explode your table rows, so CROSS JOIN should likely be a subquery. Think a bit about the row counts: how many distinct vendors, product names are there (x)? How many customers are there (y). Before your final group by you should have the product of those two queries (x\*y).
131
137
138
+
<divalign="center">-</div>
132
139
133
140
#### INSERT
134
141
1. Create a new table "product_units". This table will contain only products where the `product_qty_type = 'unit'`. It should use all of the columns from the product table, as well as a new column for the `CURRENT_TIMESTAMP`. Name the timestamp column `snapshot_timestamp`.
135
142
136
143
2. Using `INSERT`, add a new row to the product_unit table (with an updated timestamp). This can be any product you desire (e.g. add another record for Apple Pie).
137
144
145
+
<divalign="center">-</div>
138
146
139
147
#### DELETE
140
148
1. Delete the older record for the whatever product you added.
141
149
142
150
**HINT**: If you don't specify a WHERE clause, [you are going to have a bad time](https://imgflip.com/i/8iq872).
143
151
152
+
<divalign="center">-</div>
144
153
145
154
#### UPDATE
146
155
1. We want to add the current_quantity to the product_units table. First, add a new column, `current_quantity` to the table using the following syntax.
0 commit comments