Posted by Yogita, Nov. 26, 2021, 2:06 p.m.
#78 : Why COUNT(rental_id) is giving wrong/different result from COUNT(*) used in the solution?
Am getting wrong quartile for customer_id 9. Am getting a quartile 2 whereas it should be 1. When I change my COUNT(rental_id) to COUNT(*) OR COUNT(DISTINCT rental_id) I get the right answer. Am not sure why!
If there are duplicates in the rental table, shouldn't COUNT(*) be giving the same wrong answer?
My solution is :
WITH rental_per_cust AS
(SELECT c.customer_id,
max(c.store_id) AS store_id,
COUNT(rental_id) AS cnt
FROM customer c
JOIN rental r ON c.customer_id = r.customer_id
GROUP BY c.customer_id
)
SELECT *
FROM
(SELECT customer_id,
store_id,
NTILE(4) OVER (PARTITION BY store_id ORDER BY cnt) AS quartile
FROM rental_per_cust) x
WHERE customer_id IN (1,2,3,4,5,6,7,8,9,10);
If there are duplicates in the rental table, shouldn't COUNT(*) be giving the same wrong answer?
My solution is :
WITH rental_per_cust AS
(SELECT c.customer_id,
max(c.store_id) AS store_id,
COUNT(rental_id) AS cnt
FROM customer c
JOIN rental r ON c.customer_id = r.customer_id
GROUP BY c.customer_id
)
SELECT *
FROM
(SELECT customer_id,
store_id,
NTILE(4) OVER (PARTITION BY store_id ORDER BY cnt) AS quartile
FROM rental_per_cust) x
WHERE customer_id IN (1,2,3,4,5,6,7,8,9,10);