Skip to content

Assignment two#2

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

Assignment two#2
Andyisthebest wants to merge 2 commits intomainfrom
assignment-two

Conversation

@Andyisthebest
Copy link
Copy Markdown
Owner

What changes are you trying to make? (e.g. Adding or removing code, refactoring existing code, adding reports)

Adding a PDF file for the first part and modified some sql files.

What did you learn from the changes you have made?

More sql coding!

Was there another approach you were thinking about making? If so, what approach(es) were you thinking of?

NA

Were there any challenges? If so, what issue(s) did you face? How did you overcome it?

The first part of the assignment took me very long

How were these changes tested?

I searched some sql coding online

A reference to a related issue in your repository (if applicable)

Checklist

  • I can confirm that my changes are working as intended

Copy link
Copy Markdown

@anjali-deshpande-hub anjali-deshpande-hub left a comment

Choose a reason for hiding this comment

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

Well done!
I have added a few corrections. Kindly go over them.

ORDER BY market_date
) AS visit_number
FROM customer_purchases
WHERE market_date < '2022-04-29';
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good so far. You also want to add "GROUP BY customer_id, market_date" so the result set will give you 89 rows instead of 170 rows




--vi = vendor_inventory, pu = product_units
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The following query will give syntax error because the aliases are not defined.

quantity,
ROW_NUMBER() OVER (PARTITION BY product_id ORDER BY market_date DESC) AS rn
FROM vendor_inventory
)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The subquery should give the alias vi that is used in WHERE clause below.

ROW_NUMBER() OVER (PARTITION BY product_id ORDER BY market_date DESC) AS rn
FROM vendor_inventory
)
WHERE vi.product_id = pu.product_id
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is no 'pu' alias. Instead use:
WHERE vi.product_id = product_units.product_id

WHERE vi.product_id = pu.product_id
AND vi.rn = 1
)
WHERE pu.product_qty_type = 'unit';
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is no 'pu' alias. Instead use:
WHERE product_units.product_qty_type = 'unit';


DELETE FROM product_units
WHERE product_id = 1006057758
AND snapshot_timestamp < CURRENT_TIMESTAMP;
Copy link
Copy Markdown

@anjali-deshpande-hub anjali-deshpande-hub Apr 8, 2026

Choose a reason for hiding this comment

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

Your query correctly filters by product and uses a timestamp condition. Think about how to select the oldest row dynamically instead of hard coding the value. A better a way would be to find the oldest record (e.g., using MIN(snapshot_timestamp)) and delete only that row.

GROUP BY
vendor_id,
product_id;
--END QUERY
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Your second subquery (customers for the CROSS JOIN) is correct. Try to first form distinct vendor - product combinations before applying the CROSS JOIN.
Remember that the output should show vendor_name and product_name, not IDs according to the requirement. So you have to join vendor and product tables to retrieve those distinct names.

) AS purchase_count
FROM customer_purchases
WHERE market_date < '2022-04-29';
--END QUERY
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Well done! Here also add ORDER BY customer_id, product_id, market_date

Copy link
Copy Markdown

@anjali-deshpande-hub anjali-deshpande-hub left a comment

Choose a reason for hiding this comment

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

Well done. 64/70

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