Ecto Model - subquery in select - phoenix-framework

I need to make this SQL query with Ecto:
SELECT users.*, (select count(0) from money_transactions where from_id = users.id AND created_at > '2016-1-25 0:00:00.000000') as money_transactions_today_db FROM "users" WHERE "users"."client_token" = '123'
I try to do something like this but it doesn't work:
query = from users in Like4uElixir.User,
where: users.client_token in ^tokens,
select: {users, (from money_transactions in Like4uElixir.MoneyTransaction,
where: money_transactions.from_id == users.id,
select: count(0))}
Does Ecto support subqueries? If not, how can I execute the query?

You can use query fragments:
query = from users in Like4uElixir.User,
where: users.client_token in ^tokens,
select: {users, (fragment("(SELECT COUNT(0) FROM money_transactions
WHERE money_transactions.from_id == ?)", users.id))}
Although in this case the query can also be written using regular joins and group_by. Current support for subqueries in Ecto is limited.

Related

How to convert sql query mysql using criteriaquery

How to convert sql query mysql using criteriaquery this code:
SELECT a.customer as customer, a.cbgroup, a.cbdept, a.buc, a.customer_group, a.product, a.valuta, a.a_amount, a.cbgroup, b.b_amount, (b.b_amount-a.a_amount) as percentage
FROM (
SELECT a.customer, a.date, a.cbgroup, a.cbdept, a.buc, a.customer_group, a.product, a.valuta, SUM(a.amount) as a_amount FROM summaryfee as a WHERE a.date = '2019-06-30' AND a.cbgroup = 'CORPORATE BANKING 1' GROUP BY a.customer
) AS a INNER JOIN
(
SELECT b.customer, SUM(b.amount) as b_amount FROM summaryfee as b WHERE b.date = '2020-06-30' AND b.cbgroup = 'CORPORATE BANKING 1' GROUP BY b.customer
) AS b
ON a.customer=b.customer AND (b.b_amount-a.a_amount) < 0 ORDER BY CASE WHEN a.customer NOT LIKE '%Transaksi Nasabah%' THEN 0
ELSE 1
END, percentage asc LIMIT 5
You can rewrite a JPQL query as a criteria query, but not any native SQL query.
The problem at a glance with your query above is that uses subqueries in FROM clause.
JPQL query language and criteria query at least up to 2.1 JPA spec, support subqueries only in WHERE and HAVING clauses.

Optimize multiple subselects with WITH clause in Oracle

I have a query like:
select
qsn.code,
(select prs.display_name from prs where prs.id = qsn.fk_prs) display_name,
(select prs.address from prs where prs.id = qsn.fk_prs) address,
(select prs.tel from prs where prs.id = qsn.fk_prs) tel
from
qsn
where
qsn.register_date between :x1 and :x2
When I look at the execution plan of the query, it queries prs table 3 times (each time using INDEX UNIQUE SCAN).
I wonder if I can query the prs table once using WITH clause? How can I write the query that way.
I shall mention that because each of the tables have millions of record, joining them makes the query so slow.
using with clause your query goes like this:
with abc as (select id,
display_name ,
address ,
tel
from prs)
select
qsn.code,
abc.display_name,
abc.address,
abc.tel
from qsn
inner join abc
on qsn.fk_prs = abc.id
where qsn.register_date between :x1 and :x2 ;
ps: not tested.
Use a join:
select qsn.code, prs.display_name, prs.address, prs.tel
from qsn
left join prs on prs.id = qsn.fk_prs
where qsn.register_date between :x1 and :x2

How to get TOP 1 result from a LINQ left join without APPLY

I have a master table and I intend to use a left join with LINQ.
Unfortunately the left join multiplies the result (I need only a top 1 result from that).
Here comes the problem: my query should have SQL 8 conformance.
So when I use the following query:
var query = from user in context.User
join group in context.Groups on user.ID equals group.GroupID into groupJoin
from subGroup in groupJoin.Take(1).DefaultIfEmpty()
select new
{
Name = user.Name,
GroupName = subGroup!=null ? subGroup.Name : null
};
I get this exception:
The execution of this query requires the APPLY operator, which is not
supported in versions of SQL Server earlier than SQL Server 2005.
How could I replace my query to have SQL8 conformance?
Unfortunately I have no EF4 to test (it does the trick in the latest EF6), but you can try to force usage of a SQL subquery expression(s) like this:
var query = from user in context.User
select new
{
Name = user.Name,
GroupName = (from g in context.Groups where g.GroupId == user.Id select g.Name).FirstOrDefault()
};
Used your linq-query without take(1) and after that use next code:
query = query.Take(1);

how to write this oracle query in jpa?

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

SQL to LINQ query asp.net

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.

Resources