r/DatabaseHelp • • Apr 19 '16

Average of every N rows, with SQLite?

Lets say for table:

Col1Col2
4Bar
2Foo
6Baz
9Banana
10FooBar
5Apple

It would return

Col1(This is math, not actual return)
3(4+2)/2
7.5(6+9)/2
7.5(10+5)/2

I can do this with backend code w/o problems, but letting SQ do most if data-related work, is better idea, right?

2 Upvotes

9 comments sorted by

View all comments

Show parent comment

2

u/alinroc Apr 20 '16

(At least in theory. It may happen to be consistent with a given implementation of a database.)

Even then, you're rolling the dice. I've seen queries in SQL Server produce different ordering due to:

  • The presence/absence of a NOLOCK query hint (yes, I'm well aware that it's a bad idea - working on getting a vendor in line)
  • Parallelism settings (MAXDOP & Cost Threshold for Parallelism - and in this case it's not even consistent between executions of the same query, you'll potentially get different ordering for the same data & same query each time)