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
Related
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)
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']);
controlens is a table in my database and entite, state are the fields of this table
SELECT a.entite, a.etat, COUNT(a.etat) as nombre_toperform, b.nombre_performed,c.nombre_incompatible
FROM `controlens` a
LEFT JOIN ( SELECT entite,COUNT(etat) as nombre_performed from `controlens` WHERE etat like 'PERFORMED' GROUP BY entite, etat) b on a.entite = b.entite
LEFT JOIN ( SELECT entite,COUNT(etat) as nombre_incompatible from `controlens` WHERE etat like 'INCOMPATIBLE' GROUP BY entite, etat) c on a.entite = c.entite
WHERE a.etat like '%TOPERFORM%'
GROUP BY a.entite, a.etat, b.nombre_performed,c.nombre_incompatible
There are likely few ways in which you can accomplish this. Here is one idea that might work (note: haven't fully tested, so it might need some tweaks):
$toPerform = '%TOPERFORM%';
$subQuery1 = 'SELECT entite,COUNT(etat) as nombre_performed from `controlens` WHERE etat like 'PERFORMED' GROUP BY entite, etat';
$subQuery2 = 'SELECT entite,COUNT(etat) as nombre_incompatible from `controlens` WHERE etat like 'INCOMPATIBLE' GROUP BY entite, etat';
DB::table('controlens as a')->select([
'a.entite',
'a.etat',
DB::raw('COUNT(a.etat) AS nombre_toperform'),
'b.nombre_performed',
'c.nombre_incompatible'
])
->leftJoin(DB::raw("($subQuery1) as b"), 'a.entite', '=', 'b.entite')
->leftJoin(DB::raw("($subQuery2) as c"), 'a.entite', '=', 'c.entite')
->where('a.etat', $toPerform)
->groupBy('a.entite', 'a.etat', 'b.nombre_performed', 'c.nombre_incompatible');
Alternatively, you could try this package to see if it helps the cause:
eloquent-subquery-magic
Beyond that, here are a few helpful articles that might point you in the correct direction:
subquery with join in
laravel
Laravel Fluent Query Builder Join with
subquery
How to write this (left join, subquery ) in Laravel
5.1?
Why can't I use a subquery factoring clause result in the where clause of as depicted in the following sql:
with rpt as(
select * from reports where caseid =
:case_id and rownum=1 order by created desc
)
select
distinct rt.trialid
from
report_trials rt
join
trial_genes tg on rt.id=tg.trialid
where
rt.reportid = rpt.id
and
tg.gene not in('TMB','MS')
The subquery is named rptand used in the select statement's where clause. When executed encountering the following error: ORA-00904: "RPT"."ID": invalid identifier
UPDATE:
In fact nested query for the same thing is also giving me the same issue. The nested subquery is only returning a single column value from a single row:
select
distinct rt.trialid
from
report_trials rt
join
trial_genes tg on rt.id=tg.trialid
where
rt.reportid = (select id from reports where caseid = :case_id and
rownum=1 order by created desc)
and
tg.gene not in('TMB','MS')
You missed to add the table rpt in your query, thus that error.
with rpt as(
select * from reports where caseid =
:case_id and rownum=1 order by created desc
)
select
distinct rt.trialid
from
report_trials rt
join
trial_genes tg on rt.id=tg.trialid
join
rpt on rt.reportid = rpt.id
where
tg.gene not in('TMB','MS')
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