Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Q1: It's not apparently from the way the schema is shown whether "name" is a unique key. The suggested SQL will produce incorrect results in the case of a duplicate name as the window framing defaults to RANGE UNBOUNDED PRECEDING, so following peer rows are included in the aggregation.


True. Well observed. How would you resolve this?


I'm not an expert in window functions and I don't know what's idiomatic. I think explicitly specifying ROWS UNBOUNDED PRECEDING makes it do the right thing, but as you noted regarding ORDER BY, this is making people type a lot of SQL and maybe detracting from the concept. But then, this is a problem they could easily run into in the real world.

I could see perhaps having a question that comes right after this one in which the OVER ORDER BY contains duplicates, that can be used as an opportunity to teach the "gotcha".

Alternately, if there's a guarantee of uniqueness, this can't happen.. but again it might be setting people up to try to apply what they think they learned to the real world and then finding out that their sums are wrong...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: