JDOQL Subquery count problems - jdo

I am having trouble with subquery counts with JDOQL (using DataNucleus). The following query
SELECT this.price
FROM com.mysema.query.jdo.test.domain.Product
WHERE (SELECT count(other)
FROM com.mysema.query.jdo.test.domain.Product other
WHERE other.price > this.price) > a1
PARAMETERS java.lang.Long a1
causes the Exception
javax.jdo.JDOUserException: Cannot perform operation ">" on SubqueryExpression "(SELECT COUNT("OTHER".PRODUCT_ID) FROM PRODUCT "OTHER" WHERE "OTHER".PRICE > THIS.PRICE)" and IntegerLiteral "?"
at org.datanucleus.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:390)
at org.datanucleus.jdo.JDOQuery.executeWithArray(JDOQuery.java:321)
The following query works neither :
SELECT this.price
FROM com.mysema.query.jdo.test.domain.Product
WHERE !(SELECT other
FROM com.mysema.query.jdo.test.domain.Product other
WHERE other.price > this.price).isEmpty()
What is the proper way to make sure that a subquery result is not empty?

I got the problems fixed by upgrading from DataNucleus 2.0.4 to 2.1.2. The first query works.

Related

How to concatenate a Select query inside a INSTR() in SQLite?

I was trying to order a result set by the order of the values in an IN() clause.
SELECT * FROM CrossReference WHERE cross_reference_id IN (SELECT Id FROM FilteredIds)
So I tried to find a function such as MySql FIELD(). Then I found these answers (answer1, answer2) which explain how to do the exact thing on SQLite using the INSTR().
SELECT *, INSTR(',GDBR10,GDBR5,GDBR30,', ',' || ticker || ',') POS
FROM tbl
WHERE POS>0
ORDER BY POS;
So it's working as expected, but I want to populate the ids dynamically using a select query. I tried many approaches, but nothing seemed to work. Here is the last one I tried. It gave me just one result row (a result related to the first filterId).
SELECT *, INSTR (','||(SELECT id FROM FilteredIds)||',', ',' || cross_reference_id || ',') POS FROM CrossReference WHERE POS>0 ORDER BY POS;
So I guess I'm making some kind of mistake when concatenating the SELECT query with the rest of the code. Because when I manually enter the filtered Ids it works and returns results according to the entered filter ids.

Symfony3.4 / Doctrine : subquery in FROM Clause : Error: Class 'SELECT' is not defined

I'm working on a Symfony 3.4 project and I'm trying to translate an sql query to DQL query but I get an Issue.
Mysql Query:
select sum(montant_paye)
from
(select montant_paye
from vente
where client_id = 1
and montant_paye > 0
order by date ASC
limit 2)
as T;
DQL Query (Error):
return $this->getEntityManager()
->createQuery('
SELECT SUM(montantPaye) as Total
FROM
SELECT v.montantPaye
FROM AppBundle:Vente v
where v.montantPaye > 0
AND v.client = '.$clientId.'
ORDER BY v.date ASC
limit 2
')
->getResult();
Error :
[Semantical Error] line 0, col 71 near 'SELECT v.montantPaye
': Error: Class 'SELECT' is not defined.
Is any one have a solution for a correct DQL query ?
Quoting from Christophe stoef Coevoet (Symfony Core Developer):
DQL is about querying objects. Supporting subselects in the FROM clause means that the DQL parser is not able to build the result set mapping anymore (as the fields returned by the subquery may not match the object anymore).
This is why it cannot be supported (supporting it only for the case you run the query without the hydration is a no-go IMO as it would mean that the query parsing needs to be dependant of the execution mode).
In your case, the best solution is probably to run a SQL query instead
(as you are getting a scalar, you don't need the ORM hydration anyway)
Details here.
add this function to your VenteRepository:
public function sumMontantPaye($clientId)
{
return $this->createQueryBuilder("v")
->select("sum(v.montantPaye) as sum")
->where("v.client = :id")
->andWhere("v.montantPaye > 0")
->setParameter("id", $clientId)
->setMaxResults(2)
->getQuery()->getSingleResult();
}
you can access the sum using $result["sum"] assuming $result is the variable assigned to this function in the controller

Recursive Query CTE in SQL Lite

I have the Following table Structure. (I am new to SQL Lite)
create table Relations
(
Code int,
ParentCode int ,
fname text
)
GO
insert into Relations values(1,null,'A');
insert into Relations values(2,null,'B');
insert into Relations values(3,2,'C');
insert into Relations values(4,3,'D');
I want to get the initial parent of Code =4 :
i.e. values 2 null B
I am not able to figure out how to write a recursive query in sqllite.
Thanks in Advance..
Was a version issue..
This query was not working & was getting a syntax Error.
I upgraded From 3.7.17 To 3.8.7.4 version & it worked..
WITH RECURSIVE
works(Code,Parent) AS (
Select Code,ParentCode from Relations a where a.Code=4
UNION
SELECT Relations.Code, Relations.ParentCode FROM Relations , works
WHERE Relations.Code=works.Parent
)
SELECT * FROM works where Parent is null

SQlite LIMIT and [ROW_NUMBER]

In SQL this command works ok:
Query
SELECT TOP 20 * FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY t0.ProductId) AS [ROW_NUMBER], *
FROM Product AS [t0]
) AS [t1]
WHERE [t1].[ROW_NUMBER] > 0 * 20;
Now I try the same with SQLite. I know that I must replace TOP with LIMIT, but don't know where to put it.
I always get something like
Error
SQLite error near "(": syntax error" or "SQLite error near "*": syntax error".
And I am not sure that the command [ROW_NUMBER] or ROW_NUMBER() works in SQlite.
Query
SELECT *,
(
SELECT COUNT(*)
FROM Product b
WHERE a.ProductId >= b.ProductId
) AS rnum
FROM Product a LIMIT 20;
Screen Shot
See the documentation:
SELECT *
FROM Product
LIMIT 20
OFFSET 0 -- optional
SQlite doesn't support TOP. That is sql-server syntax. You have to use limit 20 instead.

Linq to Entities: Left join to get items NOT found in the join

I've got two un-related (no FK's defined) tables. The first table contains some tasks for which a user may not have access. I need to find all those tasks - in this case, the joined table would contain nulls. How do I get them?
Here's the setup:
TimeData table
- userID
- taskID
- hours
ApprovedTasks table (the one that should contain nulls)
- taskID
- userID
The SQL query would look like this:
select * from TimeData td
left join ApprovedTasks at
on at.taskID = td.taskID and at.userID = td.userID
where at.taskID is null
Any way to pull that off using a LINQ to Entity query?
TIA
Check out... Disjoint Union in LINQ
This should work...
var approvedTaks = from at in ApprovedTasks.Except(
from at2 in ApprovedTasks
where at2.userID == userId and at2.taskID==taskId
select at2)
where at.userID == userId and at.taskID==taskId
select at;
but sorry don't have the database handy to test it.

Resources