Spring-data-JPA : Join query best practices - spring

I have two tables T1 and T2.
I have to fetch the record from Table T1 where anotherColumn is null in T2 or not exists in T2.
Table T1 Entity relation with T2
#OneToMany(mappedBy="t2")
private List<T2> t2s;
Table T2 Entity relation with T1
#ManyToOne
#JoinColumn(name="pId")
private T1 t1;
In the above scenario, it should return 2nd and 3rd records from Table T1.
#Query("select t1 from T1 t1 where NOT EXISTS (select t2 from T2 t2 where t1.id = t2.pId) OR EXISTS (select t2 from T2 t2 where t1.id = t2.pId OR t2.anotherColumn=null)")
public List<T2> findDisconnected();
Since I'm using inner subqueries it is taking more time.
Could please someone helps me,
1) How can I optimize the above query?
2) What is the best way to use join queries in Spring-data-jpa?

Is this what you are looking for
select * from T1 t1 full join T2 t2 on t1.id = t2.pId where t2.anotherColumn is NULL
here you are full joining two tables and fetching all the records which have a null value in another column.

Related

USING multiple 'OR' conditions in JOIN component in Oracle data Integrator 12c

I want to use multiple 'OR' conditions in JOIN component in Oracle data Integrator 12c.
Conditions to be taken care when doing the above task is:
Say table T1 and T2, I need to take left outer join on T1(i.e. I need all the records from T1 for multiple satisfied join conditions specified in JOIN component in ODI 12c)
For example:
a. For table T1, T2: say conditions c1, c2, c3. T1 Left outer join T2.
b. I want to get the data in table say T3: Ensuring all records from T1 PLUS all records from T2 for all the conditions satisfied(namely c1,c2,c3).
Sample query:
select T1.*
from T1 LEFT OUTER JOIN T2
ON (C1 OR C2 OR C3);
Kindly help me on this at the earliest.
Thanks in advance!
You can try either query both will get you all the rows from T1 that either matched with T2 columns respectively or didn't have any match with T2.
Using UNION
SELECT DISTINCT *
FROM (
SELECT T1.*
FROM T1
LEFT OUTER JOIN T2 ON T1.day = T2.day
UNION
SELECT T1.*
FROM T1
LEFT OUTER JOIN T2 ON T1.month = T2.month
UNION
SELECT T1.*
FROM T1
LEFT OUTER JOIN T2 ON T1.yearly = T2.yearly
) as T3;
Using OR (NOTE: displaying T2 columns just to show that LEFT JOIN is working on each condition)
SELECT T1.*, T2.*
FROM T1
LEFT OUTER JOIN T2 ON
(T1.day = T2.day OR T1.month = T2.month OR T1.yearly = T2.yearly)
Sample Run
I have 4 records in T1 and 3 records in T2. Records in T1 are such that 3 rows
match with exactly 1 column in T2 and 4th row doesnt match any records in T2.
Output of both the queries gets what you need.

Hive :Insert the records that are not present

I need to insert records into a table t1 from another table t2 such that insert only the records that are not in t2.
But when i use this query
insert into table t1 select * from t2 where id not in (select id from t1);
But I get error as
Correlating expression cannot contain qualified column reference.
Can anybody suggest me a query to do this.
t2.id
Yet another ridiculous hive limitation
insert into table t1 select * from t2 where t2.id not in (select id from t1);
You can also use below command :-
insert into table t1 select t2.* from t2 left join t1 on t2.id=t1.id where t1.id is NULL;

can I use dblink in two tables in clause inner join in oracle?

For example :
select t1.*
from tabla1#dblink t1
inner join tabla2#dblink t2
on t1.campo1=t2.campo1

I want to update T1 using T3 column and by using three table relationships

UPDATE TABLE1 T1 SET T1.CENTERNAME=
(SELECT AC.CENTERNAME
FROM TABLE2 T2 INNER JOIN TABLET3 AN ON T2.CENTERID = T3.LOCATIONID
INNER JOIN TABLE1 T1 ON T3.LOG_ID = T1.LOGID W
HERE TRUNC(T1.ROW_DATE)='25-MAR-2014');
This gives the error 'ORA-01427: single-row subquery returns more than one row'.
The error message
ORA-01427: single-row subquery returns more than one row
means, er, the sub-query returns more than row. That is, this part of your statement ...
(SELECT AC.CENTERNAME
FROM TABLE2 T2 INNER JOIN TABLET3 AN ON T2.CENTERID = T3.LOCATIONID
INNER JOIN TABLE1 T1 ON T3.LOG_ID = T1.LOGID
WHERE TRUNC(T1.ROW_DATE)='25-MAR-2014')
returns more than row. The error occurs because the SET part of the UPDATE depends on the equality operator - SET T1.CENTERNAME= - so it can take only be one value.
Without more details about your data structure it is hard to be certain but I suspect what you really want is something like this
UPDATE TABLE1 T1
SET T1.CENTERNAME= (SELECT T2.CENTERNAME
FROM TABLE2 T2
INNER JOIN TABLE3 T3
ON T2.CENTERID = T3.LOCATIONID
WHERE T3.LOG_ID = T1.LOGID )
WHERE TRUNC(T1.ROW_DATE)='25-MAR-2014'
/
(I've tidied up your redaction to make the aliases consistent.)

2x COUNT in HAVING clause

I have following problem:
I want to count data in one table, count data in second table and compare countings in having clause, and display only that rows which have the same countings
Something like that:
SELECT bla
FROM T1 t1 JOIN T2 t2
ON t1.id = t2.id
HAVING COUNT(counted data from table1) = COUNT(counted data from table2)
Do you have any idea?
Cheers
Standard SQL:
SELECT t1.bla, t1.id, t1.counter, t2.counter
FROM (SELECT t1.bla, t1.id, COUNT(counted_data_from_t1) AS counter
FROM t1
GROUP BY t1.bla, t1.id
) AS t1
JOIN (SELECT t2.id, COUNT(counted_data_from_t2) AS counter
FROM t2
GROUP BY t2.id
) AS t2
ON t1.id = t2.id AND t1.counter = t2.counter
Oracle SQL (because Oracle doesn't like AS before table aliases):
SELECT t1.bla, t1.id, t1.counter, t2.counter
FROM (SELECT t1.bla, t1.id, COUNT(counted_data_from_t1) AS counter
FROM t1
GROUP BY t1.bla, t1.id
) t1
JOIN (SELECT t2.id, COUNT(counted_data_from_t2) AS counter
FROM t2
GROUP BY t2.id
) t2
ON t1.id = t2.id AND t1.counter = t2.counter
You just have to decide where bla comes from; I nominated t1. I'm assuming that for any given value of t1.id, there is a single value of t1.bla. If there isn't, then you need to explain much more clearly what you're counting and where the various columns are, and what the keys of the tables are.
Update: Apologies for not noticing the Oracle tag and giving invalid Oracle syntax.
WITH jezyki as
(SELECT pseudo_wampira, COUNT(*) AS counter
FROM Jezyki_obce_w
GROUP BY pseudo_wampira
)
,sprawnosc as
(SELECT pseudo_wampira, sprawnosc, COUNT(*) AS counter
FROM Sprawnosci_w
GROUP BY pseudo_wampira, sprawnosc
)
SELECT jezyki.pseudo_wampira, sprawnosc.counter
FROM jezyki,sprawnosc
WHERE jezyki.pseudo_wampira = sprawnosc.pseudo_wampira
AND jezyki.counter = sprawnosc.counter

Resources