Unoin clause in querydsl 4.1.3? - spring

How can I use union clause in querydsl 4.1.3?
I want just to use union clause. Need an example.
#sql
select * from (
select a,b from tableA union
select a,b from tableB
)
QueryDSL - how to join to a union of subqueries
I can't find SQLSubQuery class in querydsl lib(version 4.1.3)
I'm using
* spring boot
* spring-data
* querydsl(4.1.3)

Its perfectly possible for QueryDSL SQL queries:
QSurvey survey1 = new QSurvey("survey1");
QSurvey survey2 = new QSurvey("survey2");
QSurvey survey3 = new QSurvey("survey3");
SQLQuery<Void> query = new SQLQuery<Void>();
query.with(survey1, select(survey1.all()).from(survey1));
query.union(
select(survey2.all()).from(survey2),
select(survey3.all()).from(survey3));
The last statement returns a subquery that yields the union of the two queries.

Related

How to do union of multiple tables with where clause and order by in GORM (golang)

I'm trying to do union of multiple tables with selected columns and run where clause and order by clause on the resultset. How do I write this in GORM (Golang)
I tried the following snippet, but didn't run the where clause and order by clause in the DB query:
var union []map[string]interface{}
database.CONNECTION.Raw("? UNION ?",
database.CONNECTION.Select(ContentAttributes).Model(&model1{}),
database.CONNECTION.Select(ContentAttributes).Model(&model2{}),
).Where("id > ?", 1).Order("Name").Scan(&union)
N.B. ContentAttributes is a slice of string which contains the attributes I want to select.
It's running the following query:
SELECT "id","name","created_at","updated_at" FROM "model1" WHERE "model1"."deleted_at" IS NULL UNION SELECT "id","name","created_at","updated_at" FROM "model2" WHERE "model2"."deleted_at" IS NULL
I expected this to run the where condition and the order by clause on the union resultset. But it just did union and collected the results in the union variable. Please suggest a way to do this.
Not sure if this is the cleanest way to do it but it works.
var db = database.CONNECTION
var union []map[string]interface{}
var raw = "SELECT * (? UNION ?) union WHERE union.id > ? ORDER BY union.name"
db.Raw(raw,
db.Select("*").Model(&model1{}),
db.Select("*").Model(&model2{}),
1
).Scan(&union)

How to convert sql query mysql using criteriaquery

How to convert sql query mysql using criteriaquery this code:
SELECT a.customer as customer, a.cbgroup, a.cbdept, a.buc, a.customer_group, a.product, a.valuta, a.a_amount, a.cbgroup, b.b_amount, (b.b_amount-a.a_amount) as percentage
FROM (
SELECT a.customer, a.date, a.cbgroup, a.cbdept, a.buc, a.customer_group, a.product, a.valuta, SUM(a.amount) as a_amount FROM summaryfee as a WHERE a.date = '2019-06-30' AND a.cbgroup = 'CORPORATE BANKING 1' GROUP BY a.customer
) AS a INNER JOIN
(
SELECT b.customer, SUM(b.amount) as b_amount FROM summaryfee as b WHERE b.date = '2020-06-30' AND b.cbgroup = 'CORPORATE BANKING 1' GROUP BY b.customer
) AS b
ON a.customer=b.customer AND (b.b_amount-a.a_amount) < 0 ORDER BY CASE WHEN a.customer NOT LIKE '%Transaksi Nasabah%' THEN 0
ELSE 1
END, percentage asc LIMIT 5
You can rewrite a JPQL query as a criteria query, but not any native SQL query.
The problem at a glance with your query above is that uses subqueries in FROM clause.
JPQL query language and criteria query at least up to 2.1 JPA spec, support subqueries only in WHERE and HAVING clauses.

using subquery factoring result in where clause

Why can't I use a subquery factoring clause result in the where clause of as depicted in the following sql:
with rpt as(
select * from reports where caseid =
:case_id and rownum=1 order by created desc
)
select
distinct rt.trialid
from
report_trials rt
join
trial_genes tg on rt.id=tg.trialid
where
rt.reportid = rpt.id
and
tg.gene not in('TMB','MS')
The subquery is named rptand used in the select statement's where clause. When executed encountering the following error: ORA-00904: "RPT"."ID": invalid identifier
UPDATE:
In fact nested query for the same thing is also giving me the same issue. The nested subquery is only returning a single column value from a single row:
select
distinct rt.trialid
from
report_trials rt
join
trial_genes tg on rt.id=tg.trialid
where
rt.reportid = (select id from reports where caseid = :case_id and
rownum=1 order by created desc)
and
tg.gene not in('TMB','MS')
You missed to add the table rpt in your query, thus that error.
with rpt as(
select * from reports where caseid =
:case_id and rownum=1 order by created desc
)
select
distinct rt.trialid
from
report_trials rt
join
trial_genes tg on rt.id=tg.trialid
join
rpt on rt.reportid = rpt.id
where
tg.gene not in('TMB','MS')

Convert oracle to JPA createQuery

SELECT *
FROM
CUSTOMER_MASTER t0,
(SELECT t1.COMMNUMBER, t1.COMMTYPE FROM FOLLOWUP_GUIDES t1 WHERE (t1.STAGE_ID = '5')) t2
WHERE (t0.FOLL_UP_NUM = t2.COMMNUMBER
and T0.FOLL_UP_TYPE=t2.COMMTYPE AND (t0.COMPANY_ID = 'C001'))
In the above oracle query i have to got the follupnum and folluptype in the combination commnumber and commtype. But is it possible to write this query in JPA?
JPA fully supports SQL queries. You can use createNativeQuery(sql) or #NamedNativeQuery.
You could probably also reword your query in terms of JPQL.
If you are using EclipseLink, it does support sub-selects in the FROM clause in JPQL,
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#Sub-selects_in_FROM_clause
JPA fully supports SQL queries. You can use createNativeQuery(sql) or #NamedNativeQuery
once click this link
Assuming you have Foreign Keys in place between CUSTOMER_MASTER and FOLLOWUP_GUIDES
i.e. Assuming
CUSTOMER_MASTER.FOLL_UP_NUM is FK to FOLLOWUP_GUIDES.COMMNUMBER
CUSTOMER_MASTER.FOLL_UP_TYPE is FK to FOLLOWUP_GUIDES.COMMTYPE
Then your query
SELECT *
FROM
CUSTOMER_MASTER t0,
(SELECT t1.COMMNUMBER, t1.COMMTYPE FROM FOLLOWUP_GUIDES t1
WHERE (t1.STAGE_ID = '5')) t2
WHERE (t0.FOLL_UP_NUM = t2.COMMNUMBER
and T0.FOLL_UP_TYPE=t2.COMMTYPE AND (t0.COMPANY_ID = 'C001'))
is equivalent to
SELECT t0.*
FROM CUSTOMER_MASTER t0 JOIN FOLLOWUP_GUIDES t1
WHERE t0.COMPANY_ID = 'C001'
AND t1.STAGE_ID = '5'
This can be done in JPA quite easily and looks very similar to this SQL (only using object names and field names instead of table names and attribute names).
JPA JPQL query will look something like this:
SELECT t0
FROM CustomerMaster t0 JOIN FollowupGuides t1
WHERE t0.companyId = "C001"
AND t1.stageId = "5"
Can also do with JPA subquery:
SELECT t0
FROM CustomerMaster t0
WHERE t0.companyId = "C001"
AND EXISTS (SELECT 1
FROM FollowupGuides t1
WHERE t1.commnumber = t0.follUpNum
AND t1.commtype = t0.FollUpType
AND t1.stageId = "5")
=B)

how to use union in doctrine query symfony 1.4?

I want to have query like this in mysql:
select a.x, a.y, a.foo, sum(a.bar) as bar_sum from mytable a groupby a.x union select a.x, a.y, a.bar, sum(a.foo) as foo_sum from mytable a groupby a.y
this format is true? and how I can convert it to doctrine query in symfony?
thanks a lot
I believe that UNION is not supported using DQL (doctrine query language). But it looks like someone was able to get around the issue, using a nasty heredoc.
Or, you could just add a custom method in an object peer class and write the query in native sql.
-- Edit --
Use the following as an example (untested):
// Assuming you have the default doctrine setup, where non-object classes are
// appended with Table (your object being Foo)
class FooTable
{
public static function getFooBarUnion()
{
$sql = "select a.x, a.y, a.foo, sum(a.bar) as bar_sum from mytable a groupby a.x union select a.x, a.y, a.bar, sum(a.foo) as foo_sum from mytable a groupby a.y";
$results = Doctrine_Query::create()->query($sql);
return $results;
}
}

Resources