News for hackers. Stuff that matters.
Re:Medium-hard SQL interview questions by WoahNoun on Saturday May 02, 2020 @05:45PM (#23055178)
I use between rows for rolling averages all the time.

SUM() OVER (PARTITION BY columns ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) gives you a trailing week total.

Re:Medium-hard SQL interview questions by minimaxir on Saturday May 02, 2020 @06:12PM (#23055314)
That was incidentally the exact question I was asked. (When I asked during the interview "I know you need to use a SUM window function but I'm not sure how you constrain it to the past 7 days", the interviewer replied "you don't know the BETWEEN syntax? Everyone knows that." And then the interview ended.)
Re:Medium-hard SQL interview questions by shostack on Saturday May 02, 2020 @11:04PM (#23056586)
"In a real world situation I'd have googled it and jogged my memory in less than a minute. Since I suspect you were more interested in how I would approach solving X, can you help refresh my memory on the syntax and we can pretend I looked it up to get back to what's important here?"
Re:Medium-hard SQL interview questions by lonelappde on Saturday May 02, 2020 @07:00PM (#23055560)
You dodged a bullet with that one.
Re:Medium-hard SQL interview questions by WoahNoun on Saturday May 02, 2020 @07:04PM (#23055583)
Definitely not cool to use syntax as an interview question, but it is a very powerful tool so I wouldn't write it off because of one interview.
Re:Medium-hard SQL interview questions by truculent on Sunday May 03, 2020 @06:59AM (#23058251)
What a jerk. Sorry that happened to you.
Re:Medium-hard SQL interview questions by malinens on Sunday May 03, 2020 @03:43AM (#23057527)
sounds like he is some old guy who is afraid to loose his position and is gatekeeping so he can keep his current job...
Re:Medium-hard SQL interview questions by PeterisP on Sunday May 03, 2020 @10:12AM (#23059090)
This seems fishy - if your data has a whole day gap somewhere for whatever reason (e.g. I'm thinking sales data where certain emergencies might result in an unusual, unplanned 'zero day') then that query silently gives a wrong result, IMHO a trailing week total should always include a specific date boundary calculation to ensure that it's really a week.
Re:Medium-hard SQL interview questions by WoahNoun on Sunday May 03, 2020 @01:08PM (#23060309)
For the transactional DB sure, an analytic DB / data warehouse / or even just table would have a 0 record for the no sales day. If there are gaps in daily data, at some point they need to be filled in with 0s. You can either do that at report building or query time using a cartesian join or you can fill the nulls with 0s during the data engineering pipeline. A robust BI process should do the later.

For example, if I want to plot this trailing calculation and it has gaps in the data, then the plot will be very wrong. The DE pipeline should fill in these gaps.

Re:Medium-hard SQL interview questions by artwr on Sunday May 03, 2020 @04:31PM (#23061638)
Two ways to address this: - Join to a date dimension and coalesce nulls to 0 - use RANGE BETWEEN (only available in some DBs) on a date_difference column