Skip to content

Assignment two#2

Open
dnysius wants to merge 26 commits intomainfrom
assignment-two
Open

Assignment two#2
dnysius wants to merge 26 commits intomainfrom
assignment-two

Conversation

@dnysius
Copy link
Owner

@dnysius dnysius commented Dec 13, 2024

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

assignment two questions

What did you learn from the changes you have made?

sql querying up to window functions, insert/update

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

n/a

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

n/a

How were these changes tested?

fulfilled requirements

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

n/a

Checklist

  • I can confirm that my changes are working as intended

Copy link

@efantinatti efantinatti left a comment

Choose a reason for hiding this comment

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

Dear Dionysius Indraatmadja

Session 1:
1 - OK.

Session 2:
2 - OK.

Session 3:
3 - Partial.
-- Cross Join - Q1: Missing CROSS JOIN. Your query results are not correct.
-- DELETE - Q1: Your query deletes everything not the oldest time entry.

Final:
4 - Check the aforementioned points prior to merging your feature branch 'assignment-two' to 'main'.

Mark: 64 points.

@dnysius
Copy link
Owner Author

dnysius commented Dec 24, 2024

Dear Dionysius Indraatmadja

Session 1: 1 - OK.

Session 2: 2 - OK.

Session 3: 3 - Partial. -- Cross Join - Q1: Missing CROSS JOIN. Your query results are not correct. -- DELETE - Q1: Your query deletes everything not the oldest time entry.

Final: 4 - Check the aforementioned points prior to merging your feature branch 'assignment-two' to 'main'.

Mark: 64 points.

Hi, I'm confused why the cross join portion is not correct - I seem to be cross joining in this portion of the code:

SELECT vendor.vendor_name, product.product_name, SUM(cte.five_sales_cost) AS revenue FROM cte, cte2

Could you please clarify where the business logic is not correct?

@efantinatti
Copy link

Hi @dnysius the question asks to use CROSS JOIN clause and you are not using it. You results are not correct to what is expected.

@dnysius
Copy link
Owner Author

dnysius commented Dec 24, 2024

Hi @dnysius the question asks to use CROSS JOIN clause and you are not using it. You results are not correct to what is expected.

Hi @efantinatti , from my understanding from some online resources such as https://stackoverflow.com/questions/3918570/what-is-the-difference-between-using-a-cross-join-and-putting-a-comma-between-th using the comma as in ... FROM cte, cte2 is equivalent to ... FROM cte CROSS JOIN cte2

Could you provide me a link to a resource that explains the difference between these two?

As for the results being not correct, could you please provide further details as to what is not correct aside from the CROSS JOIN clause? I followed the hints regarding multiplying the number of vendors and customers to check the row count.

Thanks!

@efantinatti
Copy link

Hi @dnysius

Some key points:

Common Table Expressions (CTEs):

cte: Calculates five_sales_cost for each unique combination of vendor_id, product_id, and original_price from vendor_inventory. However, this CTE does not ensure the presence of distinct records before aggregation. The GROUP BY in this CTE is likely redundant unless other non-aggregated columns exist in the base table.
cte2: Extracts unique customer_id values from customer_purchases.
Cartesian Product with cte2:

The query introduces a Cartesian product between cte and cte2 because cte is not joined with cte2 in any way. This means that for every unique record in cte, the results are duplicated for each customer_id in cte2.
Joins with vendor and product:

Correctly associates vendor and product data to cte based on vendor_id and product_id.
Aggregation:

The GROUP BY aggregates results based on cte.vendor_id and cte.product_id, but the duplication from the Cartesian product inflates the results.

@dnysius
Copy link
Owner Author

dnysius commented Dec 26, 2024

Hi @efantinatti

Before the grouping and aggregation, here is what I have after the cross join/cartesian product of cte and cte2:

image

In the preview, it shows that each vendor's products has had its cost multiplied by 5, and has been joined to each distinct customer id found in customer_purchases.

In my understanding, this is what is meant by the question when it asks for the revenue per product for vendors selling 5 of each product to every customer on record. Could you let me know what is wrong with this data at this point in the query and what is the expected result?

Thanks

@efantinatti
Copy link

efantinatti commented Dec 27, 2024

Hi @dnysius

Your query is not summing up per vendor and product. Here is a simular query (based on your cte's) without CROSS JOIN that would produce the same expected result:

WITH cte1 AS (
SELECT DISTINCT
v.vendor_name,
p.product_name,
vi.original_price * 5 AS price
FROM
vendor_inventory vi
JOIN vendor v ON v.vendor_id = vi.vendor_id
JOIN product p ON p.product_id = vi.product_id
),
cte2 AS (
SELECT COUNT(DISTINCT customer_id) AS total_customers
FROM customer
)

SELECT
vendor_name,
product_name,
SUM(price) * (SELECT total_customers FROM cte2) AS sum_price
FROM
cte1
GROUP BY
vendor_name, product_name

@dnysius
Copy link
Owner Author

dnysius commented Dec 27, 2024

Hi @efantinatti

Your query produces the following result:
image

My query appears to produce the same result:
image

Since the two queries produce the same result, would it be fair to say that my query is correct? Also, I am grouping by vendor and product (using vendor.vendor_name, and product.product_name instead of ids in your query), so it should be summing up per vendor and product. Let me know if you meant something different.

If not, then could you let me know what the expected correct result should be, and I can troubleshoot from there? I'm not understanding what the issue is.

@efantinatti
Copy link

Hi @dnysius

Added ORDER BY vendor.vendor_name, product.product_name in your query from your commit 9c824c58c21a0e82c9ce7aa9cdd39fff38e2f2f8 and it seems producing the desired result.

Fixed your mark to 70 points and thanks for this discussion.

Thank you
Ernani Fantinatti

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