Query:
select *
from easquestionsinfo
where questionname in(select questionname
from easresponseinfo
where isconflict = 'yes')
This query works fine and returns me the records from table 'easquestioninfo' when questionname is equal to the one returned by the inner query which returns set of questionname where isconflict='yes'.
JPA supports JPQL, SQL, and Criteria.
You can execute this SQL directly using createNativeQuery().
For JPQL, it depends on your object model, perhaps something like,
Select q fom QuestionInfo q where q.name in (Select r.name from ResponseInfo q2 where r.isConflict = 'yes')
See,
http://en.wikibooks.org/wiki/Java_Persistence/JPQL
Related
can anybody help me to convert this SQL query to LINQ code in MVC? I need to return a list. The DB context entity is: _dbContext.
select distinct table1.AIG_ID, table1.GMT_NAME, table1.AIG_Number
from table1 left join table2 on table1.AIG_ID = table2.AIG_ID**
var data=(from item in db.table1 join
item1 in db.table2 on item.AIG_ID equals item1.AIG_ID
select new {item.AIG_ID ,item.GMT_NAME ,item.AIG_Number }).GroupBy(a=>a.AIG_ID).select(a=>a.FirstOrDefault()).ToList();
I write this part for your distinct in sql
GroupBy(a=>a.AIG_ID).select(a=>a.FirstOrDefault())
SELECT DISTINCT Title,
ProductDescription,
COUNT(1) as Duplicate
FROM DB_Deals
GROUP BY Title, ProductDescription
HAVING COUNT(1) > 1;
Well, if by EF, you mean making a query using LINQ to Entities...
from deal in context.DB_Deals
group deal by new { deal.Title, deal.ProductDescription } into dealGroup
where dealGroup.Count() > 1
select new {
dealGroup.Key.Title,
dealGroup.Key.ProductDescription,
Duplicate = dealGroup.Count(),
}
Assuming, context is your DbContext, and DB_Deals is your mapped table name.
See
Entity Framework T-Sql "having" Equivalent
Group By Multiple Columns
I'm really confusing about sub Query of hibernate.
I've standard oracle query but unable to convert it into HQL.
select distinct b.nameId
from
(
select nameId from seg_user where id=1
)a, seg_user b
where b.id=a.nameId
can somebody convert it to HQL by using SubQuery or Crieteria
select distinct b.nameId
from seg_user b
where b.id = some (
select a.nameId from seg_user a where a.id=1
)
You can see how to use subqueries here: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-subqueries
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)
I am currently trying to get some statistics for my website but i cant seem to create the query for my database to get the username that if found most frequent in all the rows.
The sql query should look something like this:
SELECT username FROM Views GROUP BY 'username' ORDER BY COUNT(*) DESC LIMIT 1
How do i make that query in my controller?
var username = db.Views.GroupBy(v => v.username).OrderByDescending(g => g.Count()).First().Key
(from a in Views
group a by a.username into b
let c = b.count()
orderby c descending
select a.username).take(1);
Your query conversion .....
This is how you do that query using LINQ:
var temp = (from a in Views
group a.username by a.username into b
orderby b.Count() descending
select b.Key).Take(1);
You can't do LIMIT 1 (mysql), since LinqToSql only generates TSql from MSSqlServer.