Oracle Sub Select with Inner Join in Laravel Eloquent way - oracle

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

Related

how to write query with sub query distinct eloquent lumen 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)

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

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 with clause error in adodb

I am not able to use the with clause in oracle adodb connection. I have tried both wrapping the qry is a select * from (myquery) and adding a , before my with clause. All create errors.
Here is the query
`WITH TEMPa AS (select _nbr, sum(ending) as Cost from t1 group by _nbr),
TEMPB AS (select _nbr, cost,entity
from t2
where end_tms is null and sale_date is null and entity = 110)
SELECT
TEMPA._nbr
,TEMPA.cost
,TEMPb._nbr
,TEMPB._cost
,tempb.entity
,(tempb._cost - tempa.cost) as Difference
FROM TEMPA, TEMPB
WHERE TEMPA._nbr = TEMPB._nbr(+)
and tempa.cost <> tempb.cost`
Any help would be awesome!

collation conflict

Is there any one who knows how we can solve collation issue in select linq query?
I'm getting this error when I want to select data in linq.
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AS" in the equal to operation
var lstData = from s in dataTrackDB.datas
join b in dataTrackDB.brandDatas on i.brandcode equals b.brandcode
join b in dataTrackDB.brandDatas on i.brandcode equals b.brandcode
join b in dataTrackDB.brandDatas on i.brandcode equals b.brandcode
join m in dataTrackDB.mktDatas on s.mktcode equals m.mktcode
select new dataView {
Account=m.account,
brandcode=b.brandcode,
commodity=s.commodity,
date=s.date,
daysvalid=s.daysvalid,
mfrcode=b.mfrcode,
mktcode=s.mktcode,
price=s.price,
prodid=s.prodid,
statecode=s.statecode,
subcommodity=s.subcommodity,
supprecode=s.supprecode,
units =s.units
};
lstData = lstData.AsQueryable().Where(x => x.mfrcode == mfr );
return lstData.Take(100).ToList();
The problem is not in Linq but in your database
you can for example create a view that joins that way and the select the data in linq from the view
SELECT * FROM T1
INNER JOIN T2 ON
T1.Name COLLATE Latin1_General_CI_AS = T2.Name COLLATE Latin1_General_CI_AS
or select the data first in linq2sql separately for each table and then join it with linq2object
add COLLATE DATABASE_DEFAULT at the end of the query

Resources