JPQL order entities by passed IDs to IN clause - jpa

is possible order entities by IDs which I pass as parameter to IN clause with spring data repository?
For instanse:
SELECT e FROM Employee WHERE e.id IN (:employeeIds);
and employeeIds = {1,2,3,4,5}
and my List with result from JPARepository will be entities is same order:
Employee={id:1, ...}, Employee={id:2, ...}, Employee={id:3, ...}

Depending on database you are using you can create a table from passed array and join the entity to is, something like that:
select e
from (values (1), (2), (3), ...) as t(id)
inner join employee e on t.id = e.id;
This can be evaluated as native query:
entityManager.createNativeQuery(
"select e " +
"from (values (1), (2), (3), ...) as t(id) " +
"inner join employee e on t.id = e.id", Employee.class)
.gerResultList();
But as you see you would have to compose query yourself or pass quite a lot od parameters (maybe in loop).

Related

How to add complex inner query to #Query annotaion

normal sql query which work correctly in db in sql developer passing values for bID and period.
SELECT * FROM A WHERE abcID IN (SELECT abcID FROM B WHERE bID=1) AND period=3
in project at Repository class I passed as this
#Query("select a from A where a.abcID IN:(select b.abcId from B where bID=:RevID) and period=:period")
error comes as
Space is not allowed after parameter prefix ':' [select a from A
where a.abcID IN:(select b.abcId from B where bID=:RevID) and
period=:period]
I want to know how should I insert above query correctly in #Query annotation
First of all I would tell you below points.
you can't use select query as select a from A where a.abcID. Here a is a column so can't define something like a.abcID. It need to be select column from tableA a where a.abcID
Same for query use in IN clause. It needs to be like select b.abcId from tableB b where b.bID=:RevID
What you use as :RevID, :period need to be passed as #Param("RevID"), #Param("period") to the query method.
This is the query template.
#Query("select a.nameOfcolumnYouWantToRetrieve from tableA a where a.someColumn in(select b.someColumn from tableB b where b.columnValueOfTableBYouwantToMatch=:RevID) and a.period=:period")
Using this points, try below query.
#Query("select a.id from A a where a.abcID in(select b.abcId from B b where b.bID =:RevID) and a.period=:period")

Sql to LINQ query LINQ Query convert

I have a SQL query I want to write in LINQ
Here is my Query
SELECT DISTINCT *
FROM [IHQDB].[dbo].[Table1] as t1
inner join Table2 as t2 on t2.Table2 =t1.ChangedItemID
inner join Table3 as t3 on t3.Table3 = t1.FromUserID
where (t1.FromUserID=1 And t2.ContentItemID= t1.ChangedItemID)
OR (t2.LastModifiedBy=1 or t2.CreatedBy=1 )
Hi now its working fine but My query little bit different on place of 1 I need my userID on base of their First Name and Last Name from M_User table.
How can I get UserId on Base of First Name + Last Name.
Here is my LINQ CODE For Retrieving User Name
linq4 = from q in context.T_ContentItems
join p in context.M_Users on q.CreatedBy equals p.UserID
where (advanceKeyword.Contains(p.EmployeeFirstName + " " + p.EmployeeLastName)) select q;
advancechk12 = linq4.ToList();
========================================================================
What I require is that wherever I have written the value "1" (e.g. t2.CreatedBy=1), I need to find the UserID. For simplicity, I am able to get the names of all the filtered users in the advancechk12. How do I retrieve the UserID's of the list of usernames returned in advancechk12
You have to replace below mentioned Linq query with your models name.I just used the same name of the T-Sql.
var t1List = (from t1 in db.Table1
join t2 in db.Table2 on t1.ChangedItemID equals t2.Id
join t3 in db.Table3 on t3.Id equals t1.FromUserID
where ((t1.FromUserID=1 && t2.ContentItemID= t1.ChangedItemID) || (t2.LastModifiedBy=1 or t2.CreatedBy=1))
select t1).Distinct().ToList();

Postgresql 9.4 Subquery missing from-clause

I have the following subquery.
In the end i want to count for every agent for all this users the controllers.
So in the substring i count the controllers for all the agent his users and
then i want to sum them up.
The substring itself is running, however i get this error when i run the complete query
ERROR: missing FROM-clause entry for table "sub"
LINE 2: a.id as a_id, SUM(sub.c_count ) as c_count,
SELECT
a.id as a_id, SUM(sub.c_count ) AS c_count,
(
SELECT u.id, COUNT(c.user_id) AS c_amount
FROM users u
JOIN controllers c ON (c.user_id = u.id)
GROUP BY
u.id
)sub
from agents a;
You are getting this error because the subquery which you have aliased as sub returns not only multiple records, but also multiple values per record, but it appears in the SELECT list. Generally, only scalars (single values) or subqueries which return a single value can appear in the SELECT list. You have two options, you can either modify sub to return a single value, or you can try to join this subauery to the outer query. Given that you mentioned the subquery is supposed to do some aggregation for each user or agent, my guess is that joining is the answer. In the query below I join sub to the outer query on the agent ID matching the user ID from sub.
SELECT a.id AS a_id,
COALESCE(sub.c_amount, 0) AS c_count
FROM agents a
LEFT JOIN
(
SELECT u.id, COUNT(c.user_id) AS c_amount
FROM users u
INNER JOIN controllers c
ON c.user_id = u.id
GROUP BY u.id
) sub
ON a.id = sub.id

Pentaho DI - How to use "all" results from prior step in the next step as an "IN" query

I have input from a tableA in database A that I would like to join to another tableB in database B.
These were my two options:
Use Database Join: For each input from table in database A, run the
join query in database B.
Use two Input tables (talbeA + tableB) and do merge join on key.
I went with option #1 as I want to avoid reading in tableA and tableB in entirety.
My question is:
How can I use all results from a prior step as one "IN" query?
For instance
select *
from tableB b
where b.id IN (all_rows_from_prior_step)
versus (where it runs for each input row)
select *
from tableB b
where b.id = ?
Use 'Group by' to flatten the rows into one row with a field 'all_rows_from_prior_step' of comma separated ids (Group field: empty, Name: all_rows_from_prior_step, Subject: id, Type: 'Concatenate strings separated by ,'). Next, use a 'User Defined Java Expression' to build the sql query:
"select * from tableB b where b.id IN (" + all_rows_from_prior_step + ")"
Last, use 'Dynamic SQL row' to run the query. The template sql could be
select * from tableB b where 1=0

MYSQL database confusion

So on this question, I'm having trouble.
EMPLOYEE(fname,minit,lname,ssn,birthdate,address,sex,salary,superssn,dno) key:ssn
DEPARTMENT(dname,dnumber,mgrssn,mgrstartdate) key:dnumber
PROJECT(pname,pnumber,plocation,dnum) key:pnumber
Here is what I wrote:
Select e.ssn, e.lname,e.fname,
From employee e,
where e.ssn in
(select s.ssn, s.lname,sfname
from employee s,
where s.superssn = e.ssn, AND s.lnamme='Wallace' s.fname ='Jennifer'
)
But I only got 10 out of 15 points, my professor said my select s.ssn,slname part is wrong, and it must "match my e.ssn". How should I fix this?
A self-join (same table). Alias e is for the worker, alias s is for the supervisor.
select s.ssn, s.lname,s.fname,
From employee s
join employee e
on s.ssn=e.superssn
where e.lname='Wallace' and e.fname ='Jennifer'
your in statement is going to make this query slow. you can refactor it to be a self join like so
select e.ssn, e.lname, e.fname
from employee e
join employee s on s.superssn = e.ssn
where s.lnamme='Wallace' AND s.fname ='Jennifer';
the problem with your in statement is you are making a dependent subquery which checks every row in the employee table with every row in the same table.
to break down the query itself
select s.ssn, s.lname, s.fname -- s is the supervisor
from employee e -- e is jennifer
join employee s on s.superssn = e.ssn -- self join on the supervisors id is equal to the employees id
where e.lnamme='Wallace' AND e.fname ='Jennifer';
Using IN is fine, but with a correlated subquery, EXISTS is the way to go:
Select s.ssn, s.lname, s.fname
From employee s
where exists (select 1
from employee e
where e.superssn = s.ssn AND
e.lname = 'Wallace' AND
e.fname = 'Jennifer'
);
Note:
The s and e are swapped. You want the supervisor information, so it goes in the outer query.
The extraneous commas have been removed.
AND has been added.
Using IN, it looks like:
Select s.ssn, s.lname, s.fname
From employee s
where s.ssn IN (select e.superssn
from employee e
where e.lname = 'Wallace' AND
e.fname = 'Jennifer'
);
Note that the correlation clause is not needed.