SQLite Forum

Question about Aggregate Function COUNT and Window Function nth_value
Login
Hi all:

For query:

------------------------------------

```SQL
CREATE TABLE v0       ( v2 INT, v1 INT );
INSERT INTO v0 VALUES ( 1     , 10      );
INSERT INTO v0 VALUES ( 1     , 10      );
INSERT INTO v0 VALUES ( 1     , 3       );
INSERT INTO v0 VALUES ( 1     , 3       );

select rowid, v2, v1, nth_value(v1, 1) over () FROM v0 ORDER BY (COUNT());
/* This SELECT stmt outputs '1|1|10|10'  */

select rowid, v2, v1 FROM v0 GROUP BY v1 ORDER BY (COUNT());
/* This SELECT stmt outputs '3|1|3    1|1|10'   */

select rowid, v2, v1 FROM v0 ORDER BY (COUNT());
/* This SELECT stmt outputs 'Error: misuse of aggregate: COUNT()' */

```

-------------------------------------

We cannot fully understand the first SELECT statement. Although this output is highly possible to be expected, we do not understand why the Window Function **nth_value** is able to rectify the error proposed by COUNT() shown in the third SELECT statement. 

We appreciate any explanation available to help us understand this first SELECT statement. Thank you.