0

Any idea why the following two queries produce the same results? According to all references I've seen, the first shouldn't even work, but it does. Obviously, if its not a glitch, I prefer the first as it's a simpler and probably faster query.

SELECT *,MIN(ts) FROM tbl GROUP BY id;

SELECT *,ts FROM tbl a WHERE rowid IN 
     (SELECT rowid FROM tbl b WHERE a.id = b.id ORDER BY ts LIMIT 1);

Here's some sample data:

rowid   id  ts  name
    1   58  10  aaa
    2   58   5  bbb
    3   72   7  ccc

result expected:

    2   58   5  bbb  5     
    3   72   7  ccc  7

SQLFiddle here...

Ryan Vincent
  • 4,483
  • 7
  • 22
  • 31
jazzgil
  • 2,250
  • 2
  • 19
  • 20

2 Answers2

1

This works because some platforms that don't support windowing functions treat this construct as a windowing function -- on SQL Server, Oracle and db2 you would have to write

 min(ts) over(order by ts)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Hogan
  • 69,564
  • 10
  • 76
  • 117
1

This is guaranteed to work since SQLite 3.7.11.

As for the why: some paying customer wanted it ...

CL.
  • 173,858
  • 17
  • 217
  • 259
  • So according to [this](http://stackoverflow.com/a/4377116/3766649), only supported on android versions 4.1+. Thanks. – jazzgil Apr 23 '16 at 21:21