How to create a subquery that uses listagg in JPA repository? - oracle

Using JPA specification classes or predicate builder. How can I convert this WHERE clause?
I am using an oracle db.
WHERE (SELECT listagg(reject_cd,':') within group (order by order_no) as rejectList
FROM REJECT_TABLE WHERE ID = transactio0_ id group by id) like '%06%'

The LISTAGG function is highly specific to Oracle, and is not supported by JPQL. However, you can still use a native query here, e.g.
#Query(
value = "SELECT ... WHERE (SELECT LISTAGG(reject_cd,':') WITHIN GROUP (ORDER BY order_no) AS rejectList FROM REJECT_TABLE WHERE ID = transactio0_ id GROUP BY id) LIKE '%06%'"
nativeQuery = true)
Collection<SomeEntity> findAllEntitiesNative();
Another option here might be to find a way to avoid needing to use LISTAGG. But, we would need to see the full query along with sample data to better understand your requirement.

Related

hibernate query language inline view (from subquery)

I know that I can't use inline views in HQL, but my company forces me to use them and think about workarounds. Maybe exist any workaround? maybe can create temporary tables for storing some result?
https://docs.jboss.org/hibernate/stable/core.old/reference/en/html/queryhql-subqueries.html
Note that HQL subqueries may occur only in the select or where clauses.
select t.quantity, i.isin, i.ticker, i.description from (
select tr.instrument_id, sum(tr.quantity) as quantity
from Transaction tr
where status = 'DONE'
group by tr.instrument_id
) t
inner join Instrument i on (t.instrument_id = i.id)

Symfony3 multiple update with subquery

Is there ability to make a multiple update with subquery on Symfony3 with Doctrine query builder or DQL?
For example, I want to run this query:
UPDATE tableA
SET fieldA2 = max_field2
FROM (SELECT
field1,
max(field2) AS max_field2
FROM table
GROUP BY field1) AS subquery
WHERE subquery.field1 = tableA.field1;
I can't understand how to use $entityManager->createQuery()->update with FROM subquery.
As far as I know, it's not possible through DQL.
You need to go through a loop.
foreach($entities as entity)
{
$em->flush();
}
Else, you will consider Batch processing, or just use plain SQL. So it might be usefull to check DBAL.

Eloquent query alternative

How can I write the following in Laravel's Eloquent?
SELECT *
FROM
( SELECT real_estate.property_id,
real_estate.amount_offered,
payee.summa
FROM real_estate
LEFT JOIN
(SELECT property_id,
SUM(amount) AS summa
FROM payments
GROUP BY property_id) payee ON payee.property_id = real_estate.property_id ) yoot
WHERE summa = 0.05 * amount_offered
Been on this for a while now and really can't get around it. Lemme explain the whole cause for the panic.
I have two tables, one for property and another for payments made on those properties. Now at any given time I will like to query for what properties have been paid for to a certain percentage hence the 0.05 which reps 5%. As it is the query works but I need an Eloquent alternative for it. Thanks
Anywhere you have subqueries in your SQL you'll need to use DB::raw with Eloquent. In this case you have a big subquery for the FROM statement, so the easiest way would be to do this:
DB::table(
DB::raw('SELECT real_estate.property_id, real_estate.amount_offered, payee.summa FROM real_estate LEFT JOIN (SELECT property_id, SUM(amount) AS summa FROM payments GROUP BY property_id) payee ON payee.property_id = real_estate.property_id)')
)
->where('summa', DB::raw('0.05 * amount_offered'))->get();
Notice I used DB::raw for the WHERE statment value as well. That's because you are doing a multiplication using a column name, and the value would otherwise be quoted as a string.
If you want to go a step further and build each subquery using Eloquent, then convert it to an SQL string and injecting it using DB::raw, you can do this:
$joinQuery = DB::table('payments')
->select('property_id', 'SUM(amount) AS summa')
->groupBy('property_id')
->toSql();
$tableQuery = DB::table('real_estate')
->select('real_estate.property_id', 'real_estate.amount_offered', 'payee.summa')
->leftJoin(DB::raw('(' . $joinQuery . ')'), function ($join)
{
$join->on('payee.property_id', '=', 'real_estate.property_id');
})
->toSql();
DB::table(DB::raw('(' . $tableQuery . ')'))->where('summa', DB::raw('0.05 * amount_offered'))->get();
In this case, the second approach doesn't have any benefits over the first, except perhaps that it's more readable. However, building subqueries using Eloquent, does have it's benefitfs when you'd need to bind any variable values to the query (such as conditions), because the query will be correctly built and escaped by Eloquent and you would not be prone to SQL injection.

How to write the following MYSQL query in criteria query and hibernate query?

How can I write the criteria query and hibernate query for the following MySQL query
SELECT * FROM (SELECT * FROM outdatadetail where algorithmno="a0025_d2" and stringno=01 ORDER BY testid desc) sub_query GROUP BY subjectid;
Any suggestions.
String sql = "SELECT * FROM (SELECT * FROM outdatadetail where algorithmno='a0025_d2' and stringno=01 ORDER BY testid desc) sub_query GROUP BY subjectid;";
Session session = getSession().getSessionFactory().getCurrentSession();
Query query = session.createSQLQuery(sql);
As far as I understand after reading the documentation and looking at examples you don't need a sub-query to do what you are trying to.
Basically you write 1 query and set a projection to do the grouping.
Criteria query = currentSession.createCriteria(OutDataDetail.class);
query.setProjection(Projections.groupProperty("subjectid").as("subjectid"));
query.add(Restrictions.eq("algorithmno", "a0025_d2"));
query.add(Restrictions.eq("stringno", "01"));
query.addOrder(Order.desc("testid"));
return query.list();
The Criteria API by itself is fairly useful. But its real power comes when you start using classes like Projection, Subqueries, Order etc. in conjunction with your Criteria.
If you want to use the Criteria API with a sub-query you can do the following:
DetachedCriteria subquery = currentSession.createCriteria(OutDataDetail.class);
subquery.add(Restrictions.eq("algorithmno", "a0025_d2"));
subquery.add(Restrictions.eq("stringno", "01"));
subquery.addOrder(Order.desc("testid"));
Criteria query = currentSession.createCriteria(OutDataDetail.class);
query.setProjection(Projections.groupProperty("subjectid").as("subjectid"));
query.add(Subqueries.exists(subquery);
return query.list();
Both implementations should return a list of OutDataDetail objects (assuming that's the object you are working with).
DISCLAIMER: I have not tried any of this. It may be that this will not work for you. This answer is written based on my knowledge of working with the Criteria API and its associated classes in the past, and the Hibernate 4.1 Manual. You can see the manual section on Projections and grouping here.

select statement

my table looks like this:
If the field name contains cost or quantity for the same lineItemIds, I have to display the result as:
cost is changed from 8*1=8
(fromVal*fromVal) to 9*6=54
(toVal*toVal) for itemID 123.
any help will be appreciated.
SELECT tc.LINE_ITEM_ID ITEM_ID,
tc.FROMVAL COST_FROMVAL,
tq.FROMVAL QTY_FROMVAL,
(tc.FROMVAL*tq.FROMVAL) PROD_FROMVAL,
tc.TOVAL COST_TOVAL,
tq.TOVAL QTY_TOVAL,
(tc.TOVAL*tq.TOVAL) PROD_TOVAL,
FROM
(SELECT LINE_ITEM_ID,
FROMVAL,
TOVAL,
FROM table
WHERE FIELDNAME = 'cost') tc
JOIN (SELECT LINE_ITEM_ID,
FROMVAL,
TOVAL,
FROM table
WHERE FIELDNAME = 'quantity') tq
ON tc.LINE_ITEM_ID = tq.LINE_ITEM_ID
I would look into using product aggregate functions. You'll have to compile them yourself though, Oracle doesn't include them as system functions. http://radino.eu/2010/11/17/product-aggregate-function/
If it's just for this one case where cost or quantity are used, then you could also just use subqueries, or temporary transaction based tables.
I'd provide you with a query example, but unfortunately don't have an Oracle instance accessible presently.

Resources