Conversation
|
https://github.com/samojoo/sql/pull/new/assignment-two pls let me know if there are any issues |
There was a problem hiding this comment.
And, please further explain type 1 and type 2 SCD in relation to Customer Address table
There was a problem hiding this comment.
Sorry about that I've made the update.
Relationship Indication
One to many Relationships:
- Customer to Order
One customer can place multiple orders - Employee to Employee Shifts
One employee can work multiple shifts - Date table to order
Multiple orders can occur one date
Many to Many Relationships: - Book Entities to Order
Many Books can be included in multiple orders. - Employee to Shift
Many employees can work multiple shifts.
In summary, most of the relationships in the table are one to many relationships, besides
the book entities to order and employee to shift
05_src/assignment2_ans.sql
Outdated
| /* 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 cp.customer_id, cp.product_id, cp.market_date, count(DISTINCT date(cp2.market_date)) as total_customer_purchases |
There was a problem hiding this comment.
Here aggregate instead of using a true window function. it might return similar results, but not fully utilizing the windowed aggregation. Consider COUNT(...) OVER (PARTITION BY.... ORDER BY ...)
There was a problem hiding this comment.
Type 1 & Type 2 in relationship to customer table
- Type 1 (Overwrite): Type 1 architecture will overwrite existing data with changes.
When a customer moves, the address in the Customer table is simply updated. Only
the current address is stored, and all history of past addresses is lost. - Type 2 (History): Type 2 architecture retains changes. Each time a customer moves,
a new row is added with the effective date of change. This way, historical addresses
are preserved while still making it possible to identify the latest address
There was a problem hiding this comment.
Thanks: SELECT customer_id, product_id, market_date, COUNT(*) OVER (PARTITION BY customer_id, product_id) AS total_customer_purchases
FROM customer_purchases;
05_src/assignment2_ans.sql
Outdated
|
|
||
|
|
||
| SELECT v.vendor_name, p.product_name, | ||
| coalesce(v2.original_price,0) * 5 * (SELECT count(*) FROM customer) AS revenue |
There was a problem hiding this comment.
They already multiplied by SELECT count (*) FROM customer, the CROSS JOIN is redundant.
Consider using the CROSS JOIN itself generating the Cartesian product, then sum
There was a problem hiding this comment.
Thanks: SELECT customer_id, product_id, market_date, COUNT(*) OVER (PARTITION BY customer_id, product_id) AS total_customer_purchases
FROM customer_purchases;
There was a problem hiding this comment.
SELECT v.vendor_name, p.product_name, sum(coalesce(v2.original_price,0) * 5) AS revenue
FROM vendor_inventory v2
JOIN vendor v ON v.vendor_id = v2.vendor_id
JOIN product p ON p.product_id = v2.product_id
CROSS JOIN customer c
GROUP BY v.vendor_name, P.product_name
ORDER BY v.vendor_name, P.product_name, revenue DESC;
05_src/assignment2_ans.sql
Outdated
| /*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 (10, 'Eggs', '1 dozen', 6, 'unit', '2025-08-17 22:44:08'); |
There was a problem hiding this comment.
instead of a hardcoded timestamp, you may consider using CURRENT_TIMESTAMP
There was a problem hiding this comment.
INSERT INTO product_units
VALUES (10, 'Eggs', '1 dozen', 6, 'unit', CURRENT_TIMESTAMP);
05_src/assignment2_ans.sql
Outdated
| HINT: If you don't specify a WHERE clause, you are going to have a bad time.*/ | ||
| DELETE FROM product_units | ||
| WHERE product_name = 'Eggs' | ||
| AND ( SELECT MIN(snapshot_timestamp) |
There was a problem hiding this comment.
Syntax error. Please consider
AND snapshot_timestamp = (SELECT snaphot_timestamp FROM ....)
There was a problem hiding this comment.
DELETE FROM product_units
WHERE product_name = 'Eggs'
AND snapshot_timestamp = ( SELECT MIN(snapshot_timestamp)
FROM product_units
WHERE product_name ='Eggs');
What changes are you trying to make? (e.g. Adding or removing code, refactoring existing code, adding reports)
What did you learn from the changes you have made?
Was there another approach you were thinking about making? If so, what approach(es) were you thinking of?
Were there any challenges? If so, what issue(s) did you face? How did you overcome it?
How were these changes tested?
A reference to a related issue in your repository (if applicable)
Checklist