Query with composite key and string_to_array - postgresql

I want to query both value route and is_deleted.
This is the query i come up with
select *
from route r
where (r.route, r.is_deleted) = any(string_to_array('ABBEYARD, VICTORIA, AUSTRALIA (CBR), false',';'));
But i got error
SQL Error [42883]: ERROR: operator does not exist: record = text
Hint: No operator matches the given name and argument type(s). You might need to add
explicit type casts.
Position: 53
can anyone enlighten me with a proper operator?

Related

postgresql - dblink giving error as No function matches the given name and argument types. You might need to add explicit type casts

I'm trying to query records from a table resides in different dababase on the same host using dblink in psql tool, but getting this error
database1=# SELECT * FROM dblink('database2', 'SELECT id FROM users');
ERROR: function dblink(unknown, unknown) does not exist
LINE 1: SELECT * FROM dblink('database2', 'SELECT id FROM ...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
can somebody help , how can we do cross database query in psql

Postgres: how to excecute the query for Sum as it is giving error?

I am using sum function to find the total but getting error.
Here is the query:
select sum(col1)
from table_name
where col2="abc"
Error: function sum(text) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts
Assuming the text column contains text numbers, not actual integers, then it would explain the error you are seeing. You might get around this by first casting text to integer, then summing:
SELECT SUM(text::int)
FROM yourTable;

How can you use Postgresql's is contained by (#>) and contains (<#) in hibernate?

I am trying to compare a provided array with a the values of a text[] column in Postgresql using hibernate.
This seems to be kind of tricky.
What I have tried so far:
Hibernate dialect
I tried to register a function in the postgres dialect:
registerFunction("array_in_array", new SQLFunctionTemplate(StandardBasicTypes.BOOLEAN, "?1 #> ?2"));
and then try to write query as such:
from MyTable mt where (array_in_array(:arr, mt.arr)) = true
When executing the query providing a String[] parameter I get following error:
org.postgresql.util.PSQLException: ERROR: operator does not exist: bytea #> text[]
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
When executing the query providing a List parameter I get following error:
org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying #> character varying
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
It suggests to use explicit type casts, so I did. Resulting following error:
With String[]
org.postgresql.util.PSQLException: ERROR: cannot cast type bytea to text[]
and with List:
org.hibernate.dialect.function.TemplateRenderer HHH000174: Function template anticipated 1 arguments, but 2 arguments encountered
org.postgresql.util.PSQLException: The column index is out of range: 2, number of columns: 1.
Also tried to use a native query. Resulting in the same errors.
Any ideas ?

Better/shorter way to check inexistence of JSONB key

Where data is JSONB column, I want to check if the key exists, normally I use:
SELECT id FROM users WHERE length(data->>'fingerprint_id') IS NULL;
Is there any better/shorter way to do this? since other alternative give incorrect result:
> SELECT id FROM users WHERE data->>'fingerprint_id' IS NULL;
ERROR: operator does not exist: jsonb ->> boolean
LINE 1: SELECT id FROM users WHERE data->>'fingerprint_id' IS NULL;
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
> SELECT id FROM users WHERE data->>'fingerprint_id' = '';
id
----
(0 rows)
There is an explicit operator (?) for this purpose:
where data_jsonb ? 'key'
But be aware, that this might cause some DB abstraction layers (f.ex. JDBC) to falsely recognize ? as an input parameter's placeholder.
As a workaround, you could use the jsonb_exists(json, text) function directly (but your code will then depend on an undocumented function), or define a similar operator for this, like in this answer.
More details about the (data ->> 'key') IS NULL syntax can be found here.
Apparently, I just need to enclose the query with () before IS NULL
SELECT id FROM users WHERE (data->>'fingerprint_id') IS NULL;

PostGres Error When Using Distinct : postgres ERROR: could not identify an ordering operator for type record

** EDIT **
Nevermind, just needed to take out the parens...
I get this error: ERROR: could not identify an ordering operator for type record
when trying to use DISTINCT
Here's the query:
select DISTINCT(g.fielda, g.fieldb, r.type)
from fields g LEFT JOIN types r ON g.id = r.id;
And the errors:
ERROR: could not identify an ordering operator for type record
HINT: Use an explicit ordering operator or modify the query.
********** Error **********
ERROR: could not identify an ordering operator for type record
SQL state: 42883
Hint: Use an explicit ordering operator or modify the query.
As I think you've worked out, you don't want the parentheses after DISTINCT. They look like they should be parameterising DISTINCT, but they actually serve to make the query return a single column of record type instead of multiple columns. The DISTINCT operator then tries to work on the record and finds that you've not defined an ordering on that record.
If you want DISTINCT to work on a subset of your return values, use DISTINCT ON.