How to use inner join in hql using subqueries - hql

I am new to HQL.
I have a table like
Product Table
I have a query like
Query query = getEntityManager().createQuery(
"Select f from Product f where f.Type=1 and f.EmpId='" + empId + "' and f.Status IN ("+allowedStatus+")");
empId=Abc and allowedStatus = (1,2)
After hitting this query I am getting all rows whose status is 2.
My problem is I want to find only those rows whose version is largest(for all ProductId) along with status is 2.
My O/P should look like:
Final Product Table
I have written a query in SQL like:
SELECT f.*
FROM Product f
INNER JOIN
(Select ProductID, Max(ProductVersion) AS Version , EmpId from Product f
WHERE f.Type=1 and f.empId='abc' and f.Status IN (1,2)
GROUP BY ProductID, empID) AS groupedtt
ON f.ProductID = groupedtt.Product
AND f.ProductVersion = groupedtt.ProductVersion
But it is showing me error below:
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 72 [Select f from Product f inner join (Select f.ProductId, max(f.Version) As version from Product f where f.Type=1 and f.empid='abc' and f.Status IN (1,2) group by ProductId) AS groupeIt on f.ProductId = groupeIt.ProductId and f.Version = groupeIt.Version]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1679)

It's not possible to perform some kind of sub queries with HQL. One of them is the SELECT after a JOIN, as you are trying to do here:
INNER JOIN
(Select ProductID, Max(ProductVersion) AS Version , EmpId from Product f

Related

JPQL query with having oracle db

I am using jpql jpa eclipselink Following query wont work :
SELECT c FROM TableA c WHERE c.forumId = :forumId AND c.isDefault = true HAVING MAX (c.validFrom)
The error im getting "The expression is not a valid conditional expression"
The HAVING clause only works with a GROUP BY expression.
The HAVING clause allows for the results of a GROUP BY to be filtered.
Your question is:
i want o get max validFrom how can i make expression ot of this
But you can make a query without GROUP BY and HAVING to do what you want:
select c from TableA c WHERE c.validFrom = (
SELECT MAX(c2.validFrom)
FROM TableA c2
WHERE c2.Id = c.Id AND c.forumId = :forumId AND c.isDefault = true
)
If you would like to use GROUP BY and HAVING, you can do:
SELECT c FROM TableA c WHERE c.validFROM = (
SELECT MAX(validFROM)
FROM TableA
WHERE forumId = :forumId AND isDefault = true
GROUP BY validFROM
HAVING validFROM=c.validFROM
)

Only want to return rows that have duplicate unitid within group by unitid

I am working in SSRS querying against an oracle database.
So I have a data source and this report is supposed to find duplicate Work Orders based on multiple open workorders on a unique unitid. So I only want to show groups that have more then one entry as they are grouped by unitid.
SELECT
COMPSTSB.UNITID,
COMPSTSB.UNITTYPE,
ACTDEFN.ACTDESC,
ACTDEFN.ACTCODE,
HISTORY.WONO,
HISTORY.COMPFLAG,
HISTORY.ADDDTTM,
HISTORY.COMMENTS
FROM (IMSV7.COMPSTSB COMPSTSB INNER JOIN IMSV7.HISTORY HISTORY ON
COMPSTSB.COMPKEY=HISTORY.COMPKEY) INNER JOIN IMSV7.ACTDEFN ACTDEFN ON
HISTORY.ACTKEY=ACTDEFN.ACTKEY
WHERE HISTORY.COMPFLAG='Y' AND NOT (ACTDEFN.ACTCODE='DBR' OR ACTDEFN.ACTCODE='IN')
ORDER BY COMPSTSB.UNITTYPE, COMPSTSB.UNITID, HISTORY.ADDDTTM
Can anyone point me in the right direction? And yes before someone says this has been asked a million times, I did search. Point me to the million times and I will see if I feel they match my question. Ideally in the SQL I could make new column that returned a count of the unitid and I did attempt this but failed, and then I could filter on that column to remove any that only had one count. I really don't think this should be difficult but I have spent about 4 hours on it so far.
Thanks in advance!
Steven
If I understood your question right, the following should give you what's needed :
SELECT * FROM
(
SELECT
COMPSTSB.UNITID,
COMPSTSB.UNITTYPE,
ACTDEFN.ACTDESC,
ACTDEFN.ACTCODE,
HISTORY.WONO,
HISTORY.COMPFLAG,
HISTORY.ADDDTTM,
HISTORY.COMMENTS,
COUNT(1) OVER (PARTITION BY COMPSTSB.UNITID) AS numDups
FROM (IMSV7.COMPSTSB COMPSTSB INNER JOIN IMSV7.HISTORY HISTORY ON
COMPSTSB.COMPKEY=HISTORY.COMPKEY) INNER JOIN IMSV7.ACTDEFN ACTDEFN ON
HISTORY.ACTKEY=ACTDEFN.ACTKEY
WHERE HISTORY.COMPFLAG='Y' AND NOT (ACTDEFN.ACTCODE='DBR' OR ACTDEFN.ACTCODE='IN')
)a
WHERE a.numDups >1
ORDER BY COMPSTSB.UNITTYPE, COMPSTSB.UNITID, HISTORY.ADDDTTM
I expect you are concerned about having duplicate values of UNITID in the IMSV7.COMPSTSB table. If so adding this join to your query should enable ou to identify them:
JOIN (SELECT COMPSTSB.UNITID
FROM IMSV7.COMPSTSB
GROUP BY COMPSTSB.UNITID
HAVING COUNT(COMPSTSB.UNITID) > 1) dups
ON DUPS.UNITID = COMPSTSB.UNITID
JOIN IMSV7.HISTORY HISTORY
Here's the full query:
SELECT COMPSTSB.UNITID
, COMPSTSB.UNITTYPE
, ACTDEFN.ACTDESC
, ACTDEFN.ACTCODE
, HISTORY.WONO
, HISTORY.COMPFLAG
, HISTORY.ADDDTTM
, HISTORY.COMMENTS
FROM IMSV7.COMPSTSB COMPSTSB
JOIN (SELECT COMPSTSB.UNITID
FROM IMSV7.COMPSTSB
GROUP BY COMPSTSB.UNITID
HAVING COUNT(COMPSTSB.UNITID) > 1) dups
ON DUPS.UNITID = COMPSTSB.UNITID
JOIN IMSV7.HISTORY HISTORY
ON COMPSTSB.COMPKEY = HISTORY.COMPKEY
JOIN IMSV7.ACTDEFN ACTDEFN
ON HISTORY.ACTKEY = ACTDEFN.ACTKEY
WHERE HISTORY.COMPFLAG = 'Y'
AND ACTDEFN.ACTCODE NOT IN ('DBR','IN')
ORDER BY COMPSTSB.UNITTYPE
, COMPSTSB.UNITID
, HISTORY.ADDDTTM;
Since the above query didn't work you can try outer joins to similar sub queries on each of your tables and limit to only records where the outer joined table returns data. This will show you which tables in your query are cuasing your extra rows.:
SELECT COMPSTSB.UNITID
, COMPSTSB.UNITTYPE
, ACTDEFN.ACTDESC
, ACTDEFN.ACTCODE
, HISTORY.WONO
, HISTORY.COMPFLAG
, HISTORY.ADDDTTM
, HISTORY.COMMENTS
, DUPS.UNITID UNITID_DUP
, DUPS2.COMPKEY COMPKEY_DUP
, DUPS3.ACTKEY ACTKEY_DUP
FROM IMSV7.COMPSTSB COMPSTSB
JOIN IMSV7.HISTORY HISTORY
ON COMPSTSB.COMPKEY = HISTORY.COMPKEY
JOIN IMSV7.ACTDEFN ACTDEFN
ON HISTORY.ACTKEY = ACTDEFN.ACTKEY
LEFT JOIN (SELECT COMPSTSB.UNITID
FROM IMSV7.COMPSTSB
GROUP BY COMPSTSB.UNITID
HAVING COUNT(COMPSTSB.UNITID) > 1) dups
ON DUPS.UNITID = COMPSTSB.UNITID
LEFT JOIN (SELECT HISTORY.COMPKEY
FROM IMSV7.HISTORY
GROUP BY HISTORY.COMPKEY
HAVING COUNT(HISTORY.COMPKEY) > 1) dups2
ON DUPS.UNITID = COMPSTSB.UNITID
LEFT JOIN (SELECT ACTDEFN.ACTKEY
FROM IMSV7.ACTDEFN
GROUP BY ACTDEFN.ACTKEY
HAVING COUNT(ACTDEFN.ACTKEY) > 1) dups3
ON DUPS.UNITID = COMPSTSB.UNITID
WHERE HISTORY.COMPFLAG = 'Y'
AND ACTDEFN.ACTCODE NOT IN ('DBR','IN')
AND ( DUPS.UNITID IS NOT NULL OR
DUPS2.COMPKEY IS NOT NULL OR
DUPS3.ACTKEY IS NOT NULL)
ORDER BY COMPSTSB.UNITTYPE
, COMPSTSB.UNITID
, HISTORY.ADDDTTM;

Nested subquery not supported in hive

We have tried the below query in hive. but getting the error. please help me to resolve this in any other way.
select count(1) as OpenItems from issues i , issue_statuses s
where s.id = i.status_id
and s.name NOT IN ('Closed','Passed','Rejected','On
Hold','Baselined','Completed')
and i.project_id IN
(select id from projects3 from
CASE WHEN ${projectname} = 'All' then id in
(select p.id from members m, projects3 p ,users_1 u
where m.project_id = p.id and u.id = m.user_id and u.status = '1'
and u.id IN
(select u1.id from users_1 u1, Supervisor_hierarchy s1 where u1.mail = s1.email and s1.name = ${Superisorname})
group by p.id)
WHEN (${projectname} <>'All' and ${SubProject projectname} ='All') then id
IN (select id from (select id from project_closure where parent_id in (select id from projects where name = ${projectname}) group by id)a)
WHEN (${SubProject projectname}<>'All' and ${projectname}<> 'All') then id
IN (select id from(select id from project_closure where id in (select id from projects where name = ${SubProject projectname}) group by id)a)
END
order by id)
error: 6:5 Unsupported SubQuery Expression 'id': SubQuery expression refers to both Parent and SubQuery expressions and is not a valid join condition.
I know it is late but posting for anyone who face this issue.
This issue occurs when we encounter one or more of the below limitations of Hive Subqueries.
In this scenario, the reference to the parent query is used in Group By clause which comes under the 4th limitation.
Hive Subquery Limitations
These subqueries are only supported on the right-hand side of an expression.
IN/NOT IN subqueries may only select a single column.
EXISTS/NOT EXISTS must have one or more correlated predicates.
References to the parent query are only supported in the WHERE clause of the subquery.
Source: https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SubQueries

The greatest row per group - further combining the result set with left join with other related table in Hibernate - HQL. Is it possible?

There two tables in my Oracle database product and product_image. They have one-to-many relationship from product to product_image. Therefore, the relationship can be mapped in Hibernate something like the following.
The product entity:
#Entity
#Table(name = "PRODUCT", catalog = "", schema = "WAGAFASHIONDB")
public class Product implements java.io.Serializable
{
#OneToMany(mappedBy = "prodId", fetch = FetchType.LAZY)
private Set<ProductImage> productImageSet;
}
#Entity
#Table(name = "PRODUCT_IMAGE", catalog = "", schema = "WAGAFASHIONDB")
public class ProductImage implements java.io.Serializable
{
#ManyToOne(fetch = FetchType.LAZY)
private Product prodId;
}
I need to query that can fetch a list of rows with the maximum prod_image_id (primary key of the prduct_image table) from each group of products in the product_image table.
This was my previous question. This can be done with the following SQL.
SELECT
pi.prod_image_id,
pi.prod_id, pi.prod_image
FROM
product_image pi
INNER JOIN (
SELECT
MAX(pi.prod_image_id) AS prod_image_id
FROM
product_image pi
GROUP BY
pi.prod_id
) prod_image
ON pi.prod_image_id=prod_image.prod_image_id
The answer to that question corresponds to the following correct HQL.
SELECT
pi.prodImageId,
pi.prodId
FROM
ProductImage pi
WHERE
pi.prodImageId in (
SELECT
MAX(pis.prodImageId)
FROM
Product p
INNER JOIN
p.productImageSet pis
GROUP BY
p.prodId
)
This brings the following result exactly as intended.
PROD_IMAGE_ID PROD_ID PROD_IMAGE
662 284 3562298873030291049_Winter.jpg
644 283 7551758088174802741_9392401244_SS_2505.jpg
595 124 298082252715152799_SS_5012.jpg
566 62 7826143854352037374_SS_5004-A.jpg
But what I actually need is that the result set retrieved by the above SQL/HQL needs to be combined with the product table with LEFT OUTER JOIN so that it can retrieve each product from the product table regardless of their images in the product_image table something like the following.
PROD_IMAGE_ID PROD_ID PROD_IMAGE
662 284 3562298873030291049_Winter.jpg
644 283 7551758088174802741_9392401244_SS_2505.jpg
595 124 298082252715152799_SS_5012.jpg
- 101 -
- 81 -
566 62 7826143854352037374_SS_5004-A.jpg
This could be done by the following native SQL but doesn't seem possible with HQL which allows a subquery only in the SELECT and the WHERE clauses, a subquery in the FROM clause is disallowed in HQL.
SELECT
t.prod_image_id,
p.prod_id,
t.prod_image
FROM
product p
LEFT OUTER JOIN(
SELECT
pi.prod_image_id,
pi.prod_id,
pi.prod_image
FROM
product_image pi
INNER JOIN (
SELECT
MAX(pi.prod_image_id) AS prod_image_id
FROM
product_image pi
GROUP BY
pi.prod_id
) prod_image
ON pi.prod_image_id=prod_image.prod_image_id
)t ON p.prod_id=t.prod_id ORDER BY p.prod_id DESC;
My Google search says this is not feasible with a single HQL statement. Is this possible somehow with HQL? Please confirm me.
You're right, you can't use subqueries in from clause.
http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#queryhql-subqueries
But you can use a separate query like:
select p
from Product p
where p.productImageSet is empty
to find the products that has no product image.
I would create an Oracle view that implements your query that gives you what you want with a simple select in HQL.
I'm currently having the same table relationship in MySQL. I'm currently trying to learn JPA. Accordingly, I'm using JPA 2.0 provided by Hibernate - version 4.2.7 final. The following JPQL,
select
p.prodId,
pi.productImageId,
p.prodName,
pi.prodImage
from
Product p
left join
p.productImageSet pi
where
pi.productImageId in (
select
max(productImageId) as productImageId
from
ProductImage
group by
prodId
)
or pi.productImageId is null
order by p.prodId
and the corresponding criteria query,
CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaQuery<Tuple>criteriaQuery=criteriaBuilder.createTupleQuery();
Metamodel metamodel=entityManager.getMetamodel();
EntityType<Product>entityType=metamodel.entity(Product.class);
Root<Product>root=criteriaQuery.from(entityType);
SetJoin<Product, ProductImage> join = root.join(Product_.productImageSet, JoinType.LEFT);
List<Selection<?>>selections=new ArrayList<Selection<?>>();
selections.add(root.get(Product_.prodId));
selections.add(root.get(Product_.prodName));
selections.add(join.get(ProductImage_.prodImage));
selections.add(join.get(ProductImage_.productImageId));
criteriaQuery.multiselect(selections);
Subquery<Long>subquery=criteriaQuery.subquery(Long.class);
Root<ProductImage> subRoot = subquery.from(ProductImage.class);
subquery.select(criteriaBuilder.max(subRoot.get(ProductImage_.productImageId)));
subquery.groupBy(subRoot.get(ProductImage_.prodId).get(Product_.prodId));
Predicate []predicates=new Predicate[2];
predicates[0]=criteriaBuilder.in(join.get(ProductImage_.productImageId)).value(subquery);
predicates[1]=join.get(ProductImage_.productImageId).isNull();
criteriaQuery.where(criteriaBuilder.or(predicates));
criteriaQuery.orderBy(criteriaBuilder.desc(root.get(Product_.prodId)));
TypedQuery<Tuple> typedQuery = entityManager.createQuery(criteriaQuery);
List<Tuple> tuples = typedQuery.getResultList();
Both of them, JPQL and criteria query produce the following SQL query.
select
product0_.prod_id as col_0_0_,
productima1_.product_image_id as col_1_0_,
product0_.prod_name as col_2_0_,
productima1_.prod_image as col_3_0_
from
social_networking.product product0_
left outer join
social_networking.product_image productima1_
on product0_.prod_id=productima1_.prod_id
where
productima1_.product_image_id in (
select
max(productima2_.product_image_id)
from
social_networking.product_image productima2_
group by
productima2_.prod_id
)
or productima1_.product_image_id is null
order by
product0_.prod_id desc
resulting in fetching the desired result set as mentioned in the question - not in the way we can see in the usual RDBMS systems but it works.
This can be done, if the query statement is slightly modified. Accordingly the following criteria query does what exactly is needed.
CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaQuery<ProductUtils>criteriaQuery=criteriaBuilder.createQuery(ProductUtils.class);
Metamodel metamodel = entityManager.getMetamodel();
Root<Product> root = criteriaQuery.from(metamodel.entity(Product.class));
ListJoin<Product, ProductImage> join = root.join(Product_.productImageList, JoinType.LEFT);
List<Selection<?>>selections=new ArrayList<Selection<?>>();
selections.add(root.get(Product_.prodId));
selections.add(root.get(Product_.prodName));
selections.add(join.get(ProductImage_.prodImage));
selections.add(join.get(ProductImage_.productImageId));
criteriaQuery.select(criteriaBuilder.construct(ProductUtils.class, selections.toArray(new Selection[0])));
Subquery<Long> subquery = criteriaQuery.subquery(Long.class);
Root<ProductImage> prodImageRoot = subquery.from(metamodel.entity(ProductImage.class));
subquery.select(prodImageRoot.get(ProductImage_.productImageId));
subquery.where(criteriaBuilder.equal(root, prodImageRoot.get(ProductImage_.prodId)), criteriaBuilder.lessThan(join.get(ProductImage_.productImageId), prodImageRoot.get(ProductImage_.productImageId)));
criteriaQuery.where(criteriaBuilder.exists(subquery).not());
criteriaQuery.orderBy(criteriaBuilder.desc(root.get(Product_.prodId)));
List<ProductUtils> list = entityManager.createQuery(criteriaQuery).getResultList();
This criteria query produces the following desired SQL query that does equally the same function which is needed as specified in the question.
select
product0_.prod_id as col_0_0_,
product0_.prod_name as col_1_0_,
productima1_.prod_image as col_2_0_ ,
productima1_.product_image_id as col_3_0_
from
social_networking.product product0_
left outer join
social_networking.product_image productima1_
on product0_.prod_id=productima1_.prod_id
where
not (exists (select
productima2_.product_image_id
from
social_networking.product_image productima2_
where
product0_.prod_id=productima2_.prod_id
and productima1_.product_image_id<productima2_.product_image_id))
order by
product0_.prod_id desc

need help on sql query

am a newbie to Oracle/PL SQL.I've 2 tables A and B.
A has a column CustId,Age,Location and Date. Table B has 2 columns CustId,CustName.
What would be the sql query to show show CustName and Location for a given age?
Thanks.
your question "What would be the sql query to show show CustName and Location for a given age?" helps define your query pretty well:
SELECT CustName, Location
FROM TableA a
INNER JOIN TableB b
ON b.CustId = a.CustId
WHERE a.Age = #
All we need to do on top of that select for your specific fields is make sure to join the two tables on their common column (CustID).
Another option would be to avoid the WHERE statement:
SELECT CustName, Location
FROM TableB b
INNER JOIN TableA a
ON a.CustID = b.CustID
AND a.Age = #
you need join. something like
SELECT custname, location FROM a JOIN b ON a.custid = b.custid WHERE age = [age];

Resources