Forum

Posted by Cyn, Feb. 1, 2022, 10:30 p.m.

#14 why my subquery returns only 7 results?

Hi, my subquery only returns dates between 5/25 and 5/31 while its supposed to spit out every single day in May. I don't know what I did wrong. Any help is appreciated. Thx!
WITH cte AS (
SELECT DATE(rental_ts) AS dt, COUNT(rental_id) AS rentalcounts FROM rental
WHERE EXTRACT(YEAR FROM rental_ts) = '2020' AND EXTRACT(MONTH FROM rental_ts) = '05'
GROUP BY DATE(rental_ts))
SELECT (SELECT COUNT(*) FROM cte WHERE rentalcounts > 100) AS good_days,
(SELECT COUNT(*) FROM cte WHERE rentalcounts <= 100) AS bad_days

Answers

Hi Cyn,
That's actually the tricky part (by design), imagine you just started your business on May 1st, and you probably didn't have paid customers for some time. Some days there could be no transactions at all.
Hope it makes sense and please let us know if you have any further questions.
Leon

Hi Leon, thank you for the explanation!! Now I get it.

Cyn, Feb. 2, 2022, 8:52 p.m.

That makes sense, but why is the expected result showing 6 good and 25 bad when data is 6 good 1 bad?

Can't complete this q as is I don't think.

Craig, July 5, 2022, 8:37 a.m.

@Craig, that's the tricky part of the question, if there were no transactions on a certain day, there would be no data.

Leon, July 5, 2022, 9:16 a.m.

aha! thanks for your reply :)

Craig, July 5, 2022, 12:39 p.m.
SQLPad user avatar

Leon (949)

Feb. 2, 2022, 9:36 a.m.