I tried to build the following sql statement through jpa CriteriaBuilder, but didn't find the right way to use it for me。
SELECT * from table WHERE string_to_array(column, ',') #> array['array_item']
Related
As a follow-up to this question:
How to "PERFORM" CTE queries in PL/pgSQL?
I try:
perform (with test_as_cte as(select * from myTable) select * from test_as_cte);
But get the following error:
SQL Error [42601]: ERROR: subquery must return only one column
Where: PL/pgSQL function inline_code_block line 9 at PERFORM
If I replace * with myCol in the above code there is no error.
However, I need to do realistic performance testing with the CTE and return multiple columns.
The WITH query enclosed in parentheses is treated like a sub-select. It works fine the way you have it as long as it returns a single value (one column of one row). Else you must treat it as subquery and call it like this (inside a PL/pgSQL code block!):
PERFORM * FROM (with test_as_cte as (select * from b2) select * from test_as_cte t) sub;
Or just:
PERFORM FROM (<any SELECT query>) sub;
The manual:
PERFORM query;
This executes query and discards the result. Write the query
the same way you would write an SQL SELECT command, but replace the
initial keyword SELECT with PERFORM. For WITH queries, use
PERFORM and then place the query in parentheses. (In this case, the
query can only return one row.)
I think this could be clearer. I'll suggest a fix for the documentation.
SELECT * FROM work_hour where start_time between '2020-04-06 23:03' and '2020-04-09 23:03';
This works when set search_path to foo
But Ii want to use the same query instead of strings I want to use parameters
So i'm using this annotation in my service function:
#Query(value = "SELECT s FROM foo.work_hour s WHERE TO_TIMESTAMP(s.start_time, 'YYYY-MM-DD HH24:mi')\\:\\:timestamp BETWEEN :start_time\\:\\:timestamp AND now()\\:\\:timestamp", nativeQuery = true)
But this seems to not work.
There was an unexpected error (type=Internal Server Error, status=500). could not execute query; SQL [SELECT s FROM logines.work_hour s WHERE TO_TIMESTAMP(s.start_time, 'YYYY-MM-DD HH24:mi')::timestamp BETWEEN ?::timestamp AND now()::timestamp]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
org.postgresql.util.PSQLException: The column name id was not found in this ResultSet.
How it can be done?
As that is a native query, you need to use proper (native) SQL syntax.
select s from work_hour s
results in a result that has a single column which is a record with multiple fields. But you want multiple columns, so you have to use
select s.*
from work_hour s
See the difference in the output in this online example
I am trying to change this oracle query to postgressql query but its not working for me:
Oracle Query:
select XMLAGG (XMLELEMENT (e, line_prefix || ',')).EXTRACT (' //text()'), ',') from in_line
Postgresql Query:
SELECT XMLAGG (XMLELEMENT ( name a_line,line_prefix||','))
FROM in_line
You don't need a workaround using XMLAGG in Postgres to create a comma separated string. This can be done using string_agg()
SELECT string_agg(line_prefix, ',')
FROM in_line
i tried to run this query in DB2 ( which includes regex ). I am getting the following error. Can someone help?
Here is the query:
SELECT COUNT(*) FROM TABLE WHERE REGEXP_LIKE(TRIM(FIELD), '[^[:digit:]]')
Support for BOOLEAN data type is new in Db2 11.1.1.1 (i.e. the first Mod Pack + Fix pack for Db2 11.1). If you are only on Db2 11.1.0.0, then you will need to explicitly test the result of your regex function.
SELECT COUNT(*) FROM TABLE
WHERE REGEXP_LIKE(TRIM(FIELD), '[^[:digit:]]') = 1;
I am trying to ues JDBC PreparedStatement to execute a SQL statement which has a question mark ? as operator. (It's the hstore ?| operator)
It throws an exception tells me that I have not specified the parameter org.postgresql.util.PSQLException: No value specified for parameter 1.. Which it should be the operator. Is there any way to by pass the parameter check and execute the statement directly?
I have seen database feature in IntelliJ can directly execute SQL with JDBC driver, I think there should be a way
There is an question (Escaping hstore contains operators in a JDBC Prepared statement) about this issue, but I really need this operator to make index working on large data sets.
Thank you
You can use SQL function inlining. A simple SQL function will get rewritten (almost always).
CREATE OR REPLACE FUNCTION hstore_contains(hstore, text[]) RETURNS boolean AS $$
SELECT $1 ?| $2;
$$ LANGUAGE SQL;
So the two queries below will get identical query plan and will both take advantage of indexes:
SELECT * FROM tbl WHERE hstore_contains(col1,array['a','b']);
SELECT * FROM tbl WHERE col1 ?| array['a','b'];