Skip to content

Assignment 2 Answers#3

Open
samojoo wants to merge 2 commits intomainfrom
assignment-two
Open

Assignment 2 Answers#3
samojoo wants to merge 2 commits intomainfrom
assignment-two

Conversation

@samojoo
Copy link
Copy Markdown
Owner

@samojoo samojoo commented Aug 18, 2025

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

  • I can confirm that my changes are working as intended

@samojoo
Copy link
Copy Markdown
Owner Author

samojoo commented Aug 18, 2025

https://github.com/samojoo/sql/pull/new/assignment-two

pls let me know if there are any issues

Copy link
Copy Markdown

@monzchan monzchan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see the comments :)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please indicate the relationship.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And, please further explain type 1 and type 2 SCD in relation to Customer Address table

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that I've made the update.
Relationship Indication
One to many Relationships:

  1. Customer to Order
    One customer can place multiple orders
  2. Employee to Employee Shifts
    One employee can work multiple shifts
  3. Date table to order
    Multiple orders can occur one date
    Many to Many Relationships:
  4. Book Entities to Order
    Many Books can be included in multiple orders.
  5. 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent!

/* 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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ...)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks: SELECT customer_id, product_id, market_date, COUNT(*) OVER (PARTITION BY customer_id, product_id) AS total_customer_purchases
FROM customer_purchases;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect answer!



SELECT v.vendor_name, p.product_name,
coalesce(v2.original_price,0) * 5 * (SELECT count(*) FROM customer) AS revenue
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks: SELECT customer_id, product_id, market_date, COUNT(*) OVER (PARTITION BY customer_id, product_id) AS total_customer_purchases
FROM customer_purchases;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful!

/*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');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of a hardcoded timestamp, you may consider using CURRENT_TIMESTAMP

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

INSERT INTO product_units
VALUES (10, 'Eggs', '1 dozen', 6, 'unit', CURRENT_TIMESTAMP);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right!

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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Syntax error. Please consider
AND snapshot_timestamp = (SELECT snaphot_timestamp FROM ....)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DELETE FROM product_units
WHERE product_name = 'Eggs'
AND snapshot_timestamp = ( SELECT MIN(snapshot_timestamp)
FROM product_units
WHERE product_name ='Eggs');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants