how to write query with sub query distinct eloquent lumen laravel - laravel

so i had a query with 2 level nested sub query. but im confused how to convert it into eloquent lumen. any helper ?
my query
SELECT b.poid,
STRING_AGG( b.epurno, '#' ) as aks
FROM (SELECT DISTINCT *
FROM (SELECT hd.poid, so.epurno
FROM trpohd hd
JOIN trpodt dt ON hd.poid = dt.poid
JOIN trsalesorder so ON dt.sohdid = so.sono
ORDER BY so.sono
) as a
) as b
GROUP BY poid)

Related

Laravel old SQL to a Laravel Eloquent query

is there a way, to get from this SQL:
SELECT *,
(SELECT count(*)
FROM addresssurveys
WHERE savedtimestamp::date = sendout_date) AS y
FROM
(SELECT created_at::date AS sendout_date,
created_at::date AS x
FROM addresssurveys
GROUP BY created_at::date) AS main
an Eloquent statement?
I created already the sub from query:
$sub = Survey::select(DB::raw('created_at::date, created_at::date as sendout_date, created_at::date as x'))->groupBy(DB::raw('created_at::date'))->orderby('created_at')->get();
But how can I combine both?
Thanks

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.

Using same table in subquery

I am failing to convert next SQL code into laravel eloquent:
SELECT t1.template, t1.created_at
FROM sent_emails t1
where created_at = (
select max(created_at) from sent_emails t2 where t2.template = t1.template
)
group by t1.created_at, t1.template
or:
SELECT t1.template, t1.created_at
FROM sent_emails t1
JOIN
(
SELECT Max(created_at) date, template
FROM sent_emails
GROUP BY template
) AS t2
ON t1.template = t2.template
AND t1.created_at = t2.date
group by t1.created_at, t1.template
Both queries return same data set. Creating subquery in separate variable is not an option as I need multiple values to be returned from it.
I also don't know how can I set alias name if I create table using models (and not using DB::), so this is my unsuccessful try:
$sent_emails = SentEmail::where('created_at', function($query) {
SentEmail::where('template', 't1.template')->orderBy('created_at', 'desc');
})->groupBy('template', 'created_at')->get(['template', 'created_at']);
You query should be something like this (I'm not at a computer to test this, so it may require further editing)
$sent_emails = SentEmail::where('created_at', function($query) {
$query->where('created_at', SentEmail::->whereColumn('column_name', 'table.column_name')->orderBy('created_at', 'desc')->max('created_at'));
})->groupBy('template', 'created_at')->get(['template', 'created_at']);

Oracle Sub Select with Inner Join in Laravel Eloquent way

I am converting Oracle SQL into Laravel. The problem is how can make the code below in Eloquent way. I am more proficient in raw SQL though.
SELECT idx, ntiIdx, readCnt
FROM (SELECT a.idx, a.ntiIdx, a.readCnt
FROM EH_APPLICANT a
INNER JOIN EH_ITEM I ON a.netIdx = i.idx AND a.userIdx = ?
ORDER BY testDate DESC, testTime DESC
)
WHERE ROWNUM = 1

subquery inside select of Laravel with Eloquent ORM with

I want to execute following raw query with laravel eloquent ORM:
SELECT *,(SELECT country FROM tbl_sim_numbers WHERE highland_id= b.highland_id) as country1,(SELECT operator FROM tbl_sim_numbers WHERE highland_id= b.highland_id) AS operator1 FROM tbl_sim_distributions b ORDER BY b.created_at
on eloquent ORM of laravel. I tried:
$sub = DB::select(DB::raw("SELECT *,(SELECT country FROM tbl_sim_numbers WHERE highland_id= b.highland_id) as country1,(SELECT operator FROM tbl_sim_numbers WHERE highland_id= b.highland_id) AS operator1 FROM tbl_sim_distributions b
ORDER BY b.created_at"));
$pdfGenObjForList = DB::table( DB::raw("({$sub->toSql()}) as sub") )
->mergeBindings($sub->getQuery());
However above code gives error.Someone,please help.

Resources