I am using doctrine 2.1 DQL
and i want to get a table data with order-by like this:
SELECT u FROM User u ORDER BY u.s * u.t
s and t are two mapped column fields on User.
But i am getting QueryException.
Can anyone please help me?
this should do the trick:
SELECT u, (u.s * u.t) AS multiplication FROM User u ORDER BY multiplication
Related
i'm having a technical and syntax problem with JOINS in ORACLE.
If i have 7 tables, listed below:
FROM
QT_QTS.PLA_ORDEM_PRODUCAO pla,
qt_qts.res_tubo_austenitizacao aust,
qt_qts.res_tubo_revenimento1 res_rev1,
qt_qts.res_tubo_revenimento2 res_rev2,
limsprod.SAMPLE sp,
limsprod.test t,
limsprod.result r
I need to get ALL the data in the "limsprod.result r" table linked with similar corresponding data inside the qt_qts.res_tubo_austenitizacao aust, qt_qts.res_tubo_revenimento1 res_rev1 and qt_qts.res_tubo_revenimento2 res_rev2 tables.
How can I do this join using Oracle Database? I tried a left join, but it did not work.
It is impossible to answer that question. We have nothing but list of some tables. I'm not sure I'd even want to do that instead of you.
However, here's a suggestion: start with one table:
select * from limsprod.result r;
It'll return all rows. Then join it to another table:
select *
from limsprod.result r join qt_qts.res_tubo_austenitizacao aust on aust.id = r.id
and see what happens - did you get all rows you want? If not, should you add another JOIN condition? Perhaps an outer join? Don't move on to the third table until you sort that out. Once you're satisfied with the result, add another table:
select *
from limsprod.result r join qt_qts.res_tubo_austenitizacao aust on aust.id = r.id
join qt_qts.res_tubo_revenimento1 res_rev1 on res_rev1.idrr = aust.idrr
Repeat what's being said previously.
I'm trying to calculate a value using DQL on one single table. Say this table is named "TABLE". It has 5 colums :
id
people_count
animal_count
region_id
type_id
The result I'm looking for is the sum of people (when grouped by region), divided by the sum of animals (when grouped by type);
The SQL would be something like that :
SELECT SUM(people_count) /
(
SELECT SUM(animal_count)
FROM TABLE t2
GROUPED BY type_id
)
FROM TABLE t1
GROUPED BY region_id
How would you do that in Doctrine using DQL?
I resolved my problem by creating another query, executing it and including the result in the first one.
This is probably not the best way to do if you are dealing with a simple example, but it was the only solution for me regarding to my code architecture.
I have a solution but I think there is probably a best solution to resolve your problem.
In any case, make two queries and import results of first query in the second can be a solution. Unfortunately, it's a low-usage for our database. More, sometimes we must execute only one SQL to sort on column result per example.
<?php
namespace AppBundle\Repository;
use AppBundle\Entity\MyClass;
use Doctrine\ORM\EntityRepository;
class MyRepository extends EntityRepository
{
public function findWithSubQuery()
{
return $this->createQueryBuilder('a')
->addSelect(sprintf(
"(SELECT COUNT(b.id) FROM %s AS b WHERE b.a = a.id GROUP BY a.id) AS otherColumn",
MyClass::class
))
;
}
}
I use this solution. Maybe the subquery could be write with DQL ojbect rather that DQL string.
I have a query joining lots of fields. For some strange reason the index for one table is not being used at all( I use the index key clearly), instead it is doing a FULL table scan. I would like to force the index. We used to do optimizer hints in sybase. Is there a similar hint available in oracle?
For example, in sybase to join tables a, b, c and use myindex in table a, I would do :
SELECT a.*
FROM a(INDEX myindex),
b,
c
WHERE a.field1 = b.field1
AND b.field1 = c.field1
Question is how do I do this in oracle.
Thanks
Saro
Yes, there is a hint like that in Oracle. It looks something like this:
select /*+ index(a my_index) */ from a
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.
My question might seem weird. But I need to know whether we can identify and seggregate the rows fetched from particular table in a query, which is formed by joining tables.
Please refer the below query if I am you are not clear with my question.
My requirement is to know the rows fetched from table 'rel' alone in the query.
SELECT rel.*
FROM rel, C
WHERE rel.col1 = C.col1
AND C.col1 NOT IN (SELECT R.col1 FROM R WHERE R.col2 = 'MN')
AND rel.col2= 'MN'
AND rel.col3= 'MN'
AND c.col2 ='MN';
Thanks,
Savitha
Since you are performing an INNER JOIN by using the comparison in the WHERE clause, all your records, by definition, are coming from the rel table.