diff --git a/02_activities/assignments/Assignment-Two-ER2.png b/02_activities/assignments/Assignment-Two-ER2.png new file mode 100644 index 000000000..09261a3b5 Binary files /dev/null and b/02_activities/assignments/Assignment-Two-ER2.png differ diff --git a/02_activities/assignments/Assignment2.md b/02_activities/assignments/Assignment2.md index 6cc76d916..79dda6cb2 100644 --- a/02_activities/assignments/Assignment2.md +++ b/02_activities/assignments/Assignment2.md @@ -56,7 +56,9 @@ The store wants to keep customer addresses. Propose two architectures for the CU ``` Your answer... ``` +The Customer Address table that will retain changes is type 2 SCD where apart from storing the Customer_ID,Customer_Name,Customer_Address_line_1 , Customer_Address_Line_2 ,City, State,Country and Pincode, the table would also have columns to track changes made to the record, such as Customer_Address_Active_Flag, Customer_Addr_Start_Date and Customer_Addr_End_date. +The Customer Address table that will overwrite changes is type 1 SCD where each record will be overwritten with columns such as the Customer_Name,Customer_Address_line_1 , Customer_Address_Line_2 ,City, State,Country and Pincode.The CustomerID column would be the primary key that cannot be overwritten. *** ## Section 2: diff --git a/02_activities/assignments/Assignment_Two_ER1.png b/02_activities/assignments/Assignment_Two_ER1.png new file mode 100644 index 000000000..231069b06 Binary files /dev/null and b/02_activities/assignments/Assignment_Two_ER1.png differ diff --git a/02_activities/assignments/assignment2.sql b/02_activities/assignments/assignment2.sql index 5ad40748a..0f834a4b6 100644 --- a/02_activities/assignments/assignment2.sql +++ b/02_activities/assignments/assignment2.sql @@ -1,4 +1,4 @@ -/* ASSIGNMENT 2 */ +/* ASSIGNMENT 2 Sucharitha S*/ /* SECTION 2 */ -- COALESCE @@ -20,6 +20,9 @@ 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.) */ +SELECT +product_name || ', ' ||coalesce (product_size, '')|| ' (' || coalesce(product_qty_type,'unit') || ')' +FROM product; --Windowed Functions @@ -32,18 +35,20 @@ each new market date for each customer, or select only the unique market dates p (without purchase details) and number those visits. HINT: One of these approaches uses ROW_NUMBER() and one uses DENSE_RANK(). */ - +select market_date,customer_id, row_number() over (partition by customer_id order by market_Date asc) as market_visit from customer_purchases +order by customer_id; /* 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. */ - +select x.customer_id, x.market_Date as recent_market_visit from +(select market_date,customer_id, row_number() over (partition by customer_id order by market_Date desc) as market_visit_count from customer_purchases) x +where x.market_visit_count = 1; /* 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. */ - - +select *,count() over (partition by customer_id,product_id) as purchased_count from customer_purchases; -- String manipulations /* 1. Some product names in the product table have descriptions like "Jar" or "Organic". @@ -57,10 +62,13 @@ Remove any trailing or leading whitespaces. Don't just use a case statement for Hint: you might need to use INSTR(product_name,'-') to find the hyphens. INSTR will help split the column. */ - +select product_name, +iif(instr(product_name,'-')=0,null,rtrim(ltrim(substr(product_name,INSTR(product_name,'-')+1))))as description +from product; /* 2. Filter the query to show any product_size value that contain a number with REGEXP. */ +select * from product where product_size regexp ( '\d'); -- UNION @@ -72,7 +80,15 @@ HINT: There are a possibly a few ways to do this query, but if you're struggling "best day" and "worst day"; 3) Query the second temp table twice, once for the best day, once for the worst day, with a UNION binding them. */ - +with sales_rank as ( +select x.market_date, x.total_sales, +dense_rank() over (order by x.total_sales asc) as sales_rank_low, +dense_rank() over (order by x.total_sales desc) as sales_rank_high from +(select sum(quantity * cost_to_customer_per_qty) as total_sales, market_date from customer_purchases group by market_date) x +) +select market_date, total_sales from sales_rank where sales_rank_low = 1 +UNION +select market_date, total_sales from sales_rank where sales_rank_high = 1; @@ -88,7 +104,17 @@ Remember, CROSS JOIN will explode your table rows, so CROSS JOIN should likely b 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). */ - +with cr_join_op as +( +select distinct vi.vendor_id, vi.product_id, v.vendor_name, p.product_name, vi.original_price, c.customer_id from +vendor_inventory vi inner join vendor v on +vi.vendor_id = v.vendor_id +inner join product p +on p.product_id = vi.product_id +cross join customer c +) +select vendor_name,product_name,sum(original_price * 5)total_sales from +cr_join_op group by vendor_name,product_name -- INSERT @@ -97,19 +123,26 @@ 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`. */ +DROP TABLE IF EXISTS temp.product_units; +CREATE TEMP TABLE product_units AS + SELECT * FROM product where product_qty_type = 'unit'; + +ALTER TABLE temp.product_units +ADD snapshot_timestamp CURRENT_TIMESTAMP; +SELECT * FROM temp.product_units; /*2. Using `INSERT`, add a new row to the product_units table (with an updated timestamp). This can be any product you desire (e.g. add another record for Apple Pie). */ - +INSERT INTO product_units +VALUES(24, 'Sweet Corn', 'Ear',1, 'unit',current_timestamp); -- DELETE /* 1. Delete the older record for the whatever product you added. - HINT: If you don't specify a WHERE clause, you are going to have a bad time.*/ - +delete from product_units where product_id =24; -- UPDATE /* 1.We want to add the current_quantity to the product_units table. @@ -128,6 +161,24 @@ Finally, make sure you have a WHERE statement to update the right row, you'll need to use product_units.product_id to refer to the correct row within the product_units table. When you have all of these components, you can run the update statement. */ +ALTER TABLE temp.product_units +ADD current_quantity INT; - - +WITH product_last_quantity AS ( + SELECT + product_id, + COALESCE(quantity, 0) AS quantity + FROM ( + SELECT + product_id, + market_date, + quantity, + ROW_NUMBER() OVER (PARTITION BY product_id ORDER BY market_date DESC) AS rn + FROM vendor_inventory + ) subquery + WHERE rn = 1 +) +UPDATE product_units +SET current_quantity = plq.quantity +FROM product_last_quantity plq +WHERE product_units.product_id = plq.product_id; \ No newline at end of file