I am using postgres DB and i have table with two column name and sal .
name Sal
Raunak 10000
Raunak 5000
Rahul 500
Raunak 300
And i want
Raunak 10000,5000,300
Rahul 500
i am using JPA is there any way to get in JPA data
You can use string_agg function to build a comma separated list of values:
select name, string_agg(sal::text, ',')
from t
group by name
You might want to consider json_agg instead of csv if your application can consume json data.
If you want to preserve the data type of the sal column, you can use array_agg() that returns an array of values. Not sure if JPA will let you access that properly though
select name, array_agg(sal) as sals
from the_table
group by name;
If I understand your question correctly, you want to get the result via below SQL statement:
SELECT
name,
string_agg (sal::varchar(22), ', ') as sals
FROM
test
GROUP BY
name;
Since it's postgresql related SQL, we can't or hard to express it via common object query. You can construct the above SQL via native query mode in your JPA code.
#Query(value = "<the above query>", nativeQuery = true)
List<Object[]> query();
Related
Table name: Employee
I have an array column name: ["a","b","c"]
and a value name -> "a" which i want to check if it is present in array.
Through sql query i am able to do using ANY clause:
SELECT *
FROM EMPLOYEE WHERE 'a' = ANY(name)
But when i try to do same through criteria query I run into issues as ANY() uses subquery as param.
Can someone help how to achieve same using criteria api without using subquery?
I have tried isMember,Any() In() using criteria query but none of that works.
I am trying to get the size of a row in a postgresql table, I found that pg_column_size would do the trick, but its not working with hibernate :
#Query("SELECT pg_column_size(t.*) as filesize FROM TABLE as t where name=:name")
int getSize(#Param("name") String name);
intellij is giving this error :
< operator > or AS expected, got '('
I guess the problem is that hibernate doesnt support specific postgresql queries, it only supports the basic sql queries.
so is there a way around this ? if not is there a way to get/estimate the size of a postgresql row in java ?
In order to use a built-in postgres function , you have to declare you JPA query as nativeQuery
you should first change query to native (hibernate will directly execute the query instead of jpa -> sql generation )
#Query(value="SELECT pg_column_size(t.*) as filesize FROM TABLE as t where name=:name",nativeQuery=true)
int getSize(#Param("name") String name);
Also be sur of the TABLE name to be correct .
Add nativeQuery = true after the native query.
#Query("SELECT pg_column_size(t.*) as filesize FROM users as t where t.name=:name",nativeQuery = true)
use above Query, Hope This will work.
I have an update/insert SQL query that I created using a MERGE statement. Using either JdbcTemplate or NamedParameterJdbcTemplate, does Spring provide a method that I can use to update a single record, as opposed to a Batch Update?
Since this query will be used to persist data from a queue via a JMS listener, I'm only dequeuing one record at a time, and don't have need for the overhead of a batch update.
If a batch is the only way to do it through Spring JDBC, that's fine... I just want to make certain I'm not missing something simpler.
You can use a SQL MERGE statment using only a one row query containing your parameters.
For example if you have a table COMPANYcontaing IDas a key and NAMEas an attribute, the MERGE statement would be:
merge into company c
using (select ? id, ? name from dual) d
on (c.id = d.id)
when matched then update
set c.name = d.name
when not matched then insert (c.id, c.name)
values(d.id, d.name)
If your target table contains the parametrised key, the name will be updated, otherwise a new record will be inserted.
With JDBCTemplate you use the update method to call the MERGEstatement, as illustrated below (using Groovy script)
def id = 1
def name = 'NewName'
String mergeStmt = """merge into company c
using (select ? id, ? name from dual) d
on (c.id = d.id)
when matched then update
set c.name = d.name
when not matched then insert (c.id, c.name)
values(d.id, d.name)""";
def updCnt = jdbcTemplate.update(mergeStmt, id, name);
println "merging ${id}, name ${name}, merged rows ${updCnt}"
Just use one of update methods, for example this one: JdbcTemplate#update instead of BatchUpdate.
Update updates a single record, batchUpdate updates multiple records using JDBC batch
I want to convert my sql query from SQL to Criterias (i dont want to use JPQL), i have this sql query:
SELECT * FROM (
SELECT CONCAT_WS(' ',p.first_name, p.middle_name, p.last_name) AS fullname FROM persons p) AS tmp
WHERE fullname LIKE '%cetina avila%' ORDER BY fullname;
How is the best way to do this?, Im trying to search full names from my talbe persons.
Thanks
One way to do this is to use an #Formula mapping on your Person class like this
#Formula("CONCAT(first_name, ' ', middle_name, ' ', last_name)")
private String fullname;
Then in your Criteria you can search it like any normal field.
See Calculated property with JPA / Hibernate for more on calculated fields.
I'm building a select that has to get me all distinct values from a table.
The sql I would normally write would look like this: "SELECT DISTINCT ARTIST FROM MUSICLIB"
However, ebean is generating the following: "SELECT DISTINCT ID, ARTIST FROM MUSICLIB"
The finder is as such:
find.select("artist").setDistinct(true).findList();
I've found that ebean is generating this ID on every single query, no matter what options I set.
How do I accomplish what I'm looking for?
You can't do that, Ebean for objects mapping requires ID field, and if you won't include it you'll get some mysterious exceptions.
Instead you can query DB without mapping and then write your SQL statement yourself:
SqlQuery sqlQuery = Ebean.createSqlQuery("SELECT DISTINCT artist FROM musiclib");
List<SqlRow> rows = sqlQuery.findList();
for (SqlRow row : rows) {
debug("I got one: " + row.getString("artist"));
}
Of course if artist is a relation, you need to perform additional query using list of found IDs with in(...) expression.