Android Room windows functions not recognized - android-sqlite

I'm using API 31 with Room v2.4.2 and SQLite v3.32.2 which is supposed to support window functions. But a simple query like this
select *, row_number() over(order by id) from mytable
produces the following error at over(
<compound operator>, FROM, GROUP, LIMIT, ORDER, WHERE, comma or semicolon expected, got '('
What's wrong with that?

Related

how to remove DISTINCT from query, in Postgresql

I have the following view:
CREATE OR REPLACE VIEW {accountId}.last_assets AS
SELECT DISTINCT ON (coin) *
from {accountId}.{tableAssetsName}
ORDER BY coin, ts DESC;
The view returns, for each coin, the latest update.
and, in an unrelated question, a comment was:
Note: DISTINCT is always a red flag. (almost always)
I read about this and it seems to have a performance issue with Postgres. In my scenario, I don't have any issues with performance, but I would like to understand:
how could such a query be rewritten to not use DISTINCT then?
There is a substantial difference between DISTINCT and the Postgres proprietary DISTINCT ON () operator.
Does whoever wrote that "DISTINCT is always a red flag" actually know the difference between DISTINCT and DISTINCT ON in Postgres?
The problem that your view solves, is known as greatest-n-per-group and in Postgres distinct on is typically more efficient than the alternatives.
The problem can be solved with e.g. window functions:
CREATE OR REPLACE VIEW {accountId}.last_assets AS
SELECT ....
FROM (
SELECT *,
row_number() over (partition by coin order by ts desc) as rn
from {accountId}.{tableAssetsName}
) t
WHERE rn = 1;
But I wouldn't be surprised if that is actually slower than you current solution.

How do I select only 1 record per user id using ROW_NUMBER without a subquery?

My current method of de-duping is really dumb.
select col1, col2 ... col500 from
(select col1, col2 ... col500, ROW_NUMBER() OVER(PARTITION BY uid) as row_num)
where row_num=1;
Is there a way to do this without a subquery? Select distinct is not an option as there can be small variations in the columns which are not significant for this output.
In Postgres distinct on () is typically faster then the equivalent solution using a window function and also doesn't require a sub-query:
select distinct on (uuid) *
from the_table
order by something
You have to supply an order by (which is something you should have done with row_number() as well) to get stable results - otherwise the chosen row is "random".
The above is true for Postgres. You also tagged your question with amazon-redshift - I have no idea if Redshift (which is in fact a very different DBMS) supports the same thing nor if it is as efficient.

Understanding a simple DISTINCT ON in postgresql

I am having a small difficulty understanding the below simple DISTINCT ON query:
SELECT DISTINCT
ON (bcolor) bcolor,
fcolor
FROM
t1
ORDER BY
bcolor,
fcolor;
I have this table here:
What is the order of execution of the above table and why I am getting the following result:
As I understand since ORDER BY is used it will display the table columns (both of them), in alphabetical order and since ON is used it will return the 1st matched duplicate, but I am still confused about how the resulting table is displayed.
Can somebody take me through how exactly this query is executed ?
This is an odd one since you would think that the SELECT would happen first, then the ORDER BY like any normal RDBMS, but the DISTINCT ON is special. It needs to know the order of the records in order to properly determine which records should be dropped.
So, in this case, it orders first by the bcolor, then by the fcolor. Then it determines distinct bcolors, and drops any but the first record for each distinct group.
In short, it does ORDER BY then applies the DISTINCT ON to drop the appropriate records. I think it would be most helpful to think of 'DISTINCT ON' as being special functionality that differs greatly from DISTINCT.
Added after initial post:
This could be done using window functions and a subquery as well:
SELECT
bcolor,
fcolor
FROM
(
SELECT
ROW_NUMBER() OVER (PARTITION BY bcolor ORDER BY fcolor ASC) as rownumber,
bcolor,
fcolor
FROM t1
) t2
WHERE rownumber = 1

Postgres reporting "window function call requires an OVER clause" in a query that has an OVER clause

I've got a table, me, that's got an e_id column, a first_name, and a last_name. (A few other columns too but I'm just trying to figure out the window function stuff.) When I try to do a query and pick out the first name values in the table for a given e_id value:
SELECT e_id, first_value(first_name), first_value(last_name)
OVER (PARTITION BY e_id)
FROM me
I get the "window function call requires an OVER clause" error. Now, like I said, I don't know what I'm doing yet, but I'm pretty sure there's at least an attempt at an OVER clause in that query. OK, so when I try it without the functions:
SELECT e_id, first_name, last_name
OVER (PARTITION BY e_id)
FROM me
I get a syntax error at OVER. I'm running psql version 9.4.4 against a 9.1.13 server. I'm staring at the documentation for 9.1 and it looks to me like OVER is documented there. Am I just missing something basic here?
Each window function must have its own OVER clause. The problem with your first query is that the first_value(first_name) window function does not have an OVER clause.
And the problem with your second query is that you have an OVER clause that is not preceded by a window function.
Try this
SELECT e_id,
first_value(first_name) OVER (PARTITION BY e_id),
first_value(last_name) OVER (PARTITION BY e_id)
FROM me

Rank() Over Partition By in MS Access

I have the following query:
SELECT opstudy, product_set, RANK() OVER (PARTITION BY opstudy ORDER BY case_pack_qty) AS CASE_PK_RANK
FROM rngr_15_grp;
that I'm using in MS Access. I have the Object Designer settings set to SQL Server syntax compatible. I'm getting
"syntax error (missing operator) in query expression RANK() OVER (PARTITION BY opstudy ORDER BY case_pack_qty)
What operator belongs in that expression?
You can't use RANK() OVER ... in Access. The setting SQL Server Compatible Syntax (ANSI 92) implies that you must use ANSI-92 syntax for all queries. The RANK() analytic window function wasn't introduced in the ANSI standard until ANSI SQL:1999 and thus can't be used in SQL-92 mode.