How to write multiple sub-query in Hibernate criteria - oracle

I am trying to write oracle subquery in Hibernate criteria but unable to do it. Can anyone help me to achieve this. Below is my oracle query.
SELECT a.id,
b.address
FROM tableA a
INNER JOIN TABLE b
ON a.id = b.id
WHERE mainId IN
(SELECT bp.ptyID
FROM bpTable bp,
busHeaderbh bh
WHERE bh.aid = bp.aid
AND bh.parentBID IN
(SELECT bp.ptyID
FROM bpTable bp,
busHeaderbh bh
WHERE bh.aid = bp.aid
AND bh.parentBID = 123
UNION
SELECT 123 FROM dual
)
UNION
SELECT 123 FROM dual
)
AND
GROUP BY a.id,
b.credttm
ORDER BY a.id DESC;
Thanks in Advance.

I have written one example for one to many relationship table
you can get reference from it
Criteria person = session.getCurrentSession().createCriteria(Person.class).createAlias("personId", "personId");
person.add(Restrictions.disjunction().add(Restrictions.ilike("PersonFirstname",Search,MatchMode.ANYWHERE))
.add(Restrictions.ilike("personId.prop1",Search,MatchMode.ANYWHERE))
.add(Restrictions.ilike("personId.col1",Search,MatchMode.ANYWHERE))
.addOrder(Property.forName("colName").desc()
.addOrder(Property.forName("colName").asc());

Related

ORACLE SQL DEVELOPER-select count(*) from multiple databases

How can I select count(*) from two different databases(call them ZEOTA and SP) having as result:
Zeota SP
88 3
I have tried this:
SELECT COUNT(CONSTRAINT_TYPE) NumberOfPrimaryKeys_Zeota
FROM ALL_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'P'
AND
OWNER = 'ZEOTA';
SELECT COUNT(*) AS NumberOfAttributes_SP
FROM ALL_COL_COMMENTS
WHERE OWNER = 'SP';
But the output shows two separate query results:
Query result 1:
Zeota
88
Query Result 2:
SP
3
However I am trying to do it this way, but having issues:
SELECT
COUNT(DISTINCT TABLE_NAME) AS ZNumOfTables,
COUNT(DISTINCT TABLE_NAME) AS SNumOfTables,
COUNT(DISTINCT TABLE_NAME) AS PNumOfTables
FROM ALL_TAB_COLUMNS
WHERE OWNER = 'SP';
WHERE OWNER = 'ZEOTA';
One option is to use your current queries as CTEs and then just cross join them:
with
t1 as
(select count(constraint_type) num_pk_zeota
from all_constraints
where owner = 'ZEOTA'
and constraint_type = 'PK'
),
t2 as
(select count(*) num_attr_sp
from all_col_comments
where owner = 'SP'
)
select a.num_pk_zeota,
b.num_attr_sp
from t1 a cross join t2 b;
P.S. What you call "databases" are users (schemas) in Oracle.

Query to retrieve rows with no data even if not satisfying the where clause condition in Oracle

I have a query two tables - TableA , TableB
Query :
select a.name, count(b.code)
from tableA a
join tableA b on b.id = a.id
where a.name = ('AA','BB','WWW')
group by a.name;
The data is available only for AA so it displays the result as
AA 5
But i want to display the data as :
AA 5
BB 0
WWW 0
You can collect expected names into separate table/subquery and then perform a left join with your data tables like
with exp_names as (
select 'AA' name from dual
union all
select 'BB' from dual
union all
select 'WWW' from dual
)
select en.name, count(b.code)
from exp_names en
left join tableA a on en.name = a.name
left join tableB b on b.id = a.id
group by en.name;
fiddle

Create Oracle database query

I have the following table (tb1):
I need to create a query that consist of:
Select the oldest Date_created having Status 001.
Should not select a PCR if the same PCR having status 002.
For the table above, this query should return the following table:
Can anyone help me how to create it?
Final query:
select q2.id,q2.PCR,q2.status, q2.date_created from (select pcr, min(date_created) date_created from table1 t1 where not exists (select * from table1 t2 where t1.pcr = t2.pcr and t2.status = '002') group by pcr) q1 inner join (select * from table1) q2 on q1.PCR = q2.PCR and q1.date_created = q2.date_created

Distinct on one column in linq with joins

I know we can get distinct on one column using following query:
I know we can get distinct on one column using following query:
SELECT *
FROM (SELECT A, B, C,
ROW_NUMBER() OVER (PARTITION BY B ORDER BY A) AS RowNumber
FROM MyTable
WHERE B LIKE 'FOO%') AS a
WHERE a.RowNumber = 1
I have used similar sql query in my case where i am joining multiple tables but my project is in mvc4 and i need linq to entity equivalent of the same. Here is my code:
select * from
(
select fp.URN_No,
ROW_NUMBER() OVER
(PARTITION BY pdh.ChangedOn ORDER BY fp.CreatedOn)
as num,
fp.CreatedOn, pdh.FarmersName, pdh.ChangedOn, cdh.Address1, cdh.State, ich.TypeOfCertificate, ich.IdentityNumber, bdh.bankType, bdh.bankName,
pidh.DistrictId, pidh.PacsRegistrationNumber, idh.IncomeLevel, idh.GrossAnnualIncome
from MST_FarmerProfile as fp inner join PersonalDetailsHistories as pdh on fp.personDetails_Id = pdh.PersonalDetails_Id
inner join ContactDetailsHistories as cdh on fp.contactDetails_Id = cdh.ContactDetails_Id
inner join IdentityCertificateHistories as ich on fp.IdentityCertificate_Id = ich.IdentityCertificate_Id
inner join BankDetailsHistories as bdh on fp.BankDetails_Id = bdh.BankDetails_Id
left join PacInsuranceDataHistories as pidh on fp.PacsInsuranceData_Id = pidh.PacsInsuranceData_Id
left join IncomeDetailsHistories as idh on fp.IncomeDetails_Id = idh.IncomeDetails_Id
where URN_No in(
select distinct MST_FarmerProfile_URN_No from PersonalDetailsHistories where MST_FarmerProfile_URN_No in(
select URN_No from MST_FarmerProfile where (CreatedOn>=#fromDate and CreatedOn<= #toDate and Status='Active')))
)a where a.num=1
Use this linq query after getting result from sql. p.ID is be your desire distinct column name
List<Person> distinctRecords = YourResultList
.GroupBy(p => new { p.ID})
.Select(g => g.First())
.ToList();

Oracle Optimizer Unexpected Results

I have a co worker who wrote the following query. The first one works and the second one does not. Also if you remove the aggregate function from the subquery, it works. The oracle optimizer is doing something weird. Any thoughts? Running in SQL Developer 3.1 against 11.1.0.6.0 64 bit.
This works:
SELECT
a.fd_customer_key
, b.fd_customer_key
, b.counter
FROM FETCH_CUSTOMER a
, (select fd_customer_key, count(*) as counter from fetch_customer_order group by fd_customer_key) b
where a.fd_customer_key = b.fd_customer_key (+)
and b.counter is null
This doesn’t:
SELECT
a.fd_customer_key
, b.fd_customer_key
, b.counter
FROM FETCH_CUSTOMER a
, (select fd_customer_key, count(*) as counter from fetch_customer_order group by fd_customer_key) b
where a.fd_customer_key = b.fd_customer_key (+)
and b.fd_customer_key is null
Actually yes, both of the queries you provided are supposed to wrok the same way, but if i understand your need well, you are trying to select the fd_customer_key which has no Order?
I suggest the following query for your need, its more simple and less consuming :
SELECT a.fd_customer_key
FROM FETCH_CUSTOMER a
WHERE NOT EXISTS (SELECT 1
FROM fetch_customer_order b
WHERE a.fd_customer_key = b.fd_customer_key)
It seems like you are trying to make an anti-join (find the rows from FETCH_CUSTOMER that have no corresponding rows in FETCH_CUSTOMER_ORDER).
With Oracle you do not have to use this clever OUTER JOIN trick to write an anti-join, you could use a NOT IN or NOT EXISTS operator and let the optimizer find the best plan. This will be just as efficient and easier to read.
Anyway, I can't reproduce your findings, here's my setup:
CREATE TABLE a (ID NUMBER PRIMARY KEY);
CREATE TABLE b (a_id NUMBER NOT NULL, DATA VARCHAR2(30));
INSERT INTO a (SELECT object_id FROM all_objects);
INSERT INTO b (SELECT object_id, object_name
FROM all_objects WHERE object_type = 'VIEW');
SELECT a.id, b.a_id, b.cnt
FROM a, (SELECT a_id, COUNT(*) cnt FROM b GROUP BY a_id) b
WHERE a.id = b.a_id (+)
AND b.cnt IS NULL;
SELECT a.id, b.a_id, b.cnt
FROM a, (SELECT a_id, COUNT(*) cnt FROM b GROUP BY a_id) b
WHERE a.id = b.a_id (+)
AND b.a_id IS NULL;
You will find that both queries return rows. What is your DB version?

Resources