Forum

Posted by Egor, Aug. 29, 2022, 1:24 a.m.

69 : Number of happy customers

Make sure you included 2020-05-31.

This question is closed

Answers

I think your main problem is a bit of data ballooning by selecting all the columns in your CTE, when really you just need the customer_id and to calculate the difference between rentals.

Also, I'm not sure it's required per se, but I would partition by the customer_id in your window function vs ordering - it makes it drastically more clear what you're trying to do vs filtering it later with the customer_id = p_c. 

Hi, Mike

Yes, thanks, I was experimenting with the possibility of not using partitioning, however, I've edited the code in the way you suggested, and it gives the same results, coz I used distinct only for customer id, hence it doesn't matter which columns are being returned from CTE(from a results perspective, ofc we should bring only data we need). 

Egor, Aug. 29, 2022, 8:54 p.m.
SQLPad user avatar

Mike (228)

Aug. 29, 2022, 8:56 a.m.

Sorry, threading isn't working in the forum at the moment so had to add a different answer.

In your CTE where clause, change the rental_ts to rental_ts::date and remove the ::timestamp from the strings, then change your primary where clause to just = instead of <=.

Thank you, Mike

Egor, Aug. 30, 2022, 7:55 p.m.

Thanks Mike! Right on! Good question Egor!

Leon, Aug. 31, 2022, 9:35 a.m.
SQLPad user avatar

Mike (228)

Aug. 30, 2022, 5:31 a.m.