Trying to find where NOT IN - subquery

I need to find my contacts that have a specific committee assignment or belong to a specific membership group that are not registered for a specific event. I tested each select separately, however it is definitely my NOT IN which is incorrect.. I keep getting an error.. The c.id (contact ID ) would be not in if the A.EventApi__Contact__c is not in the subquery ..
SELECT
DISTINCT (c.id) "ID",
c.Hidden_VIS__c "VIS Number",
c.FirstName "First Name",
c.LastName "Last Name",
CN.Name "Committee Name",
M.MEMBERSHIP_GROUP_NAME__C "Membership Name"
FROM CONTACT C
LEFT JOIN COMMITTEE_ASSIGNMENT__C CA
ON c.id=CA.CONTACT__C
LEFT JOIN COMMITTEE__C CN
ON CN.ID=CA.COMMITTEE__C
LEFT JOIN MEMBERSHIP__C M
ON M.DONOR__C=C.id
WHERE (CN.NAME='Boston Board of Directors' OR CN.NAME='Boston Board of Trustees' OR
CN.NAME='Capital Region Board of Directors')
OR (M.Status__c='Active' OR M.Status__c='Grace') AND (M.MEMBERSHIP_GROUP_NAME__C='CHAI
Society' OR M.MEMBERSHIP_GROUP_NAME__C='Sapphire')
AND c.id NOT IN (
SELECT
A.EventApi__Contact__c "Contact",
c.id
FROM EventApi__Attendee__c A
LEFT JOIN EventApi__Event__c E
ON E.ID=a.EventApi__Attendee_Event__c
Left Join CONTACT C
ON c.id=A.EventApi__Contact__c
WHERE E.Appeal_Code__c='1093' AND A.EventApi__Status__c='Registered');

Related

HYBRIS Flexible Query to get all the products from Order Model

My requirement is to get the list of customers who have ordered an old product.
Here for the old product, we are using an attribute "endproduct".
I am able to get all the customers who have placed orders. BUT I don't know how to create a query to get products from Order Model.
I have run this query :
SELECT distinct {c:uid},{aeo:product} from
{customer as c JOIN order as o on {c:pk}={o:user}JOIN AbstractOrder as ao on {o:pk}={ao:pk} JOIN AbstractOrderEntry as aeo on {ao:pk}={aeo:pk}}
Because AbstractOrderEntryModel has a product attribute.
Try like
SELECT
distinct {u:uid},{p:name}
FROM { Order AS o JOIN OrderEntry AS oe ON {o.pk} = {oe.order} JOIN Product AS p ON {p.pk} = {oe.product} and {p.endproduct} = '1' JOIN User AS u ON {o.user} = {u.pk}}
Change endproduct condition as per your requirement.
Try below query, it should give expected results.
select {c.uid},{p.code}
from {Customer as c}, {Order as o}, {Product as p}, {AbstractOrderEntry as ao}
where {o.user} = {c.pk} and {o.pk} = {ao.order} and {ao.product} = {p.pk}

Unable to get the Order and Order entry details using flex query

I am using below flexible search query to get the order and order entry details from Hybris (version1808)
select {o.code},{o.date},{o.date},{o.totalprice},{o.totalTAX}, {ev.code} from {
Order as o
JOIN OrderEntry as oe ON {oe.orderpk} = {o.PK}
JOIN EnumerationValue as ev on {o.status}={ev.pk}
JOIN Product as p ON {oe.productpk} = {p.PK}
}
where {o.versionID} is NULL
I am getting below error
cannot search unknown field 'TableField(name='orderpk',langPK='null',type=OrderEntry)' within type OrderEntry unless you disable checking
Is there anything that I am missing?
Change {oe.orderpk} to {oe.order} and {oe.productpk} to {oe.product}
SELECT {o.code},{o.date},{o.date},{o.totalprice},{o.totalTAX}, {ev.code} from {Order as o
JOIN OrderEntry as oe ON {oe.order} = {o.PK}
JOIN EnumerationValue as ev on {o.status}={ev.pk}
JOIN Product as p ON {oe.product} = {p.PK}
}
where {o.versionID} is NULL

JPQL LEFT JOIN is not working

I want to get the list of all Branch even if they have no accounts with user role
Query query = em.createQuery("SELECT NEW com.package.BranchInstructors(b,a) FROM Branch b LEFT JOIN b.accounts a WHERE b.dFlg = 0 AND a.userRole = :role ORDER BY b.name ASC");
query.setParameter("role", "user");
return query.getResultList();
Unfortunately it's returning only Branches with user role, it's like doing INNER JOIN instead.
Any idea what's going on?
just add the a.userRole is null condition to your query to avoid filtering the null userRole that you got from the left join
SELECT NEW com.package.BranchInstructors(b,a)
FROM Branch b
LEFT JOIN b.accounts a
WHERE b.dFlg = 0
AND (a.userRole = :role OR a.userRole IS NULL)
ORDER BY b.name ASC"
The problem is in your WHERE vs LEFT JOIN clause.
If you use LEFT JOIN table Accounts and use this table in the WHERE with AND condition, it behaves like a JOIN.
So, you can use WITH in the LEFT JOIN:
Query query = em.createQuery("SELECT NEW com.package.BranchInstructors(b,a) FROM Branch b
LEFT JOIN b.accounts a WITH a.userRole = :role
WHERE b.dFlg = 0 ORDER BY b.name ASC");

Count null columns as zeros with oracle

I am running a query with Oracle:
SELECT
c.customer_number,
COUNT(DISTINCT o.ORDER_NUMBER),
COUNT(DISTINCT q.QUOTE_NUMBER)
FROM
Customer c
JOIN Orders o on c.customer_number = o.party_number
JOIN Quote q on c.customer_number = q.account_number
GROUP BY
c.customer_number
This works beautifully and I can get the customer and their order and quote counts.
However, not all customers have orders or quotes but I still want their data. When I use LEFT JOIN I get this error from Oracle:
ORA-24347: Warning of a NULL column in an aggregate function
Seemingly this error is caused by the eventual COUNT(NULL) for customers that are missing orders and/or quotes.
How can I get a COUNT of null values to come out to 0 in this query?
I can do COUNT(DISTINCT NVL(o.ORDER_NUMBER, 0)) but then the counts will come out to 1 if orders/quotes are missing which is no good. Using NVL(o.ORDER_NUMBER, NULL) has the same problem.
Try using inline views:
SELECT
c.customer_number,
o.order_count,
q.quote_count
FROM
customer c,
( SELECT
party_number,
COUNT(DISTINCT order_number) AS order_count
FROM
orders
GROUP BY
party_number
) o,
( SELECT
account_number,
COUNT(DISTINCT quote_number) AS quote_count
FROM
quote
GROUP BY
account_number
) q
WHERE 1=1
AND c.customer_number = o.party_number (+)
AND c.customer_number = q.account_number (+)
;
Sorry, but I'm not working with any databases right now to test this, or to test whatever the ANSI SQL version might be. Just going on memory.

Complex sql query and JPQL

How to change this complex sql statement into JPQL?
select a.name, a.surname, b.street, c.location, c.location_desc
from table1 join table2 on b.id = a.some_fk
left join table3 d on d.id = a.id
left join table4 c on c.some2_id = d.some2_fk where a.id = 400;
If this is possible to present in the JPQL form?
Impossible to give a definitive answer without knowing the entities and their mapping, but the query would look like this:
select a.name, a.surname, b.street, c.location, c.locationDesc
from Entity1 a
join a.entity2 b
left join a.entity3 d
left join d.entity4 c
where a.id = 400;
provided the necessary associations between entities are there.
JPQL is object oriented, it operates against JPA entity objects ,not database tables. Either you need to change the Question and add an UML diagramm or provide the Entity classes.

Resources