Pagination in Oracle 11g - oracle

I am trying to achieve pagination in Oracle 11g with following query:
SELECT * FROM (SELECT ROW_NUMBER () OVER (ORDER BY
general_compliance_Details.ID) R,
general_compliance.,
get_entity.,
general_compliance_Details.,
GSTN_STATE_JURIS_DETAILS.,
etm_gl_district.*
FROM general_compliance_details
LEFT JOIN get_Entity
ON get_entity.gstin = general_compliance_details.gstin
LEFT JOIN general_compliance
ON general_compliance.id =
general_compliance_details.general_compliance_id
LEFT JOIN get_entity_status
ON get_entity_status.gstin =
general_compliance_details.gstin
LEFT JOIN GSTN_STATE_JURIS_DETAILS
ON get_Entity.PPBZDTLS_STJD = GSTN_STATE_JURIS_DETAILS.code
LEFT JOIN etm_gl_district
ON GSTN_STATE_JURIS_DETAILS.dist_cd =
etm_gl_district.district_code
WHERE general_compliance_details.general_compliance_id = '2'
AND general_compliance_details.status = '0'
AND general_compliance.form_type = '1') WHERE R >= 100 AND R <= 200
But it is giving error Exception111ORA-00918: column ambiguously defined but my all columns are defined using table.column_name.
Please help.

Related

Conversion of Oracle query to teradata query

How to convert this below query to equivalent teradate query. I tried but results varies a lot.
select il.domainN as listname, il.SourceID, ns.sourcename, cbo.customerid, cu.username, hv.domainN as HTname
, nvl((select 1
from mydb.customerPP cpp
where cbo.customerid = cpp.customerid
and NOT EXISTS (select 1 from mydb.customerPI cpt where cpp.customerid = cpt.customerid)
and trunc(cpp.startdate) <= sysdate
group by cpp.customerid),0) as BBID
from mydb.customerBO cbo
join mydb.customers cu on cbo.customerid = cu.customerid
join mydb.inv il on cbo.domainN = il.domainN
join mydb.Sources ns on il.SourceID = ns.SourceID
left join mydb2.HT hv on (il.domainN = hv.domainN
and hv.sDate+1 >= il.dDate
and il.dDate+1 >= hv.sDate)
where cbo.customerBOID = 1
and cu.statusid = 1
and il.sourceTID = 2
and il.joinbydate >= cbo.cDate
and trunc(il.dDate) = trunc(sysdate)
Thanks.
Use coalesce instead nvl and compare explain statements to see whether you have left outer joins converted to inner joins for nvl on Teradata.

Join the result with new inner query sql

select A.UNIT, A.LEASE_ID, A.MONTHS_GUARANTEED, A.MONTHLY_PAYMENT_AM,
B.DATE_PAID, C.CHARGE_JOB from leasei A
left outer join eq_capture B on A.LEASE_ID = B.LEASE_ID
left outer join eq_mast C on A.UNIT = C.UNIT
where A.DATE_LEASE_EXPIRE = 0
ORDER BY A.LEASE_ID;
I want to use the result of the above query that is use the value C.CHARGE_JOB and the check with another table (job_infojc D) and get the D.STATE value with a where condition C.CHARGE_JOB = D.JOB
Any help is highly appreciated.
Like this:
select D.STATE, X.*
from job_infojc D
join (select A.UNIT, A.LEASE_ID, A.MONTHS_GUARANTEED, A.MONTHLY_PAYMENT_AM,
B.DATE_PAID, C.CHARGE_JOB from leasei A
left outer join eq_capture B on A.LEASE_ID = B.LEASE_ID
left outer join eq_mast C on A.UNIT = C.UNIT
where A.DATE_LEASE_EXPIRE = 0
) X on X.CHARGE_JOB = D.JOB
ORDER BY X.LEASE_ID;

how to pass select subquery in where clause using laravel

This is my SQl query
select sum(stock.total_in_stock) as total_in_stock
,stock.name
,stock.inventory_id
from (
select i.store_id
,i.model_id
,i. total_in_stock
,i.id as inventory_id
, m.*
from `inventory` as `i`
left join `model_store` as `ms` on `ms`.`store_id` = `i`.`store_id`
left join `model` as `m` on `m`.`id` = `ms`.`model_id`
where `i`.`model_id` = m.id
and `m`.`status` = 1
and `ms`.`status` = 1
and `i`.`created_at` = (
select si.created_at
from inventory AS si
where si.model_id = i.model_id
and si.store_id = i.store_id
and si.status=1
order by si.created_at desc limit 1
)
) as stock
group by stock.model_id
In laravel, it is written as this:
$results1 = DB::table('inventory as i')
->select(DB::raw( 'sum(stock.total_in_stock) as total_in_stock,stock.name,stock.inventory_id FROM ( SELECT i.store_id,i.model_id,i. total_in_stock,i.id as inventory_id, m.* '))
->leftJoin('model_store as ms','ms.store_id','=','i.store_id')
->leftJoin('model as m','m.id','=','ms.model_id')
->where('i.model_id','=', 'm.id')
->where('m.status','=', '1')
->where('ms.status','=', '1')
->where("i.created_at","=",function($query) {
$query->select(DB::raw("si.created_at FROM inventory AS si WHERE si.model_id = i.model_id AND si.store_id = i.store_id AND si.status=1 ORDER BY si.created_at DESC LIMIT 1)) as stock GROUP BY stock.model_id"));
});
It gives me the following error:-
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 (SQL: select sum(stock.total_in_stock) as total_in_stock,stock.name,stock.inventory_id FROM ( SELECT i.store_id,i.model_id,i. total_in_stock,i.id as inventory_id, m.* from `inventory` as `i` left join `model_store` as `ms` on `ms`.`store_id` = `i`.`store_id` left join `model` as `m` on `m`.`id` = `ms`.`model_id` where `i`.`model_id` = m.id and `m`.`status` = 1 and `ms`.`status` = 1 and `i`.`created_at` = (select si.created_at FROM inventory AS si WHERE si.model_id = i.model_id AND si.store_id = i.store_id AND si.status=1 ORDER BY si.created_at DESC LIMIT 1 )) as stock GROUP BY stock.model_id))
It takes 2 closing brackets at the end and gives the above error. Please help me writing the above SQL query in laravel.
Answering your question that's in the tile: the subquery where needs fix:
->where("i.created_at", function($query) {
$query->from('inventory as si')
->selectRaw('max(si.created_at)')
->whereRaw('si.model_id = i.model_id AND ...');
});
However, in order to create a subquery in the from clause, you need to pass raw query, so your whole code requires a bit of toSql() and setBindings(...), which is cumbersome.
So to get the answer to your problem, better describe the problem itself instead.

How to write Count, Distinct in LINQ from Oracle

I have a query in Oracle which i am trying to convert into linq. I think I am almost there. Here is the query in Oracle. I had quick problem with left outer joins in the query. Please consider this too in the question. My main problem is I can't write using this count, distinct for different columns in the table.
SELECT COUNT(DISTINCT claimant_id || rqst_wk_dt || claim_id) AS no_of_weeks_compensated
, SUM(pmt_am) AS total_payments
, COUNT(DISTINCT claimant_id || claim_id)
FROM (SELECT c.claimant_id
, c.claim_id
, c.rqst_wk_dt
, a.pmt_am
FROM ui_mon_hdr d
INNER JOIN ui_rqst_wk_ctrl c
ON d.claimant_id = c.claimant_id
AND d.claim_id = c.claim_id
LEFT OUTER JOIN ui_dstb_pmt a
ON c.claimant_id = a.claimant_id
AND c.claim_id = a.claim_id
AND c.rqst_wk_dt = a.rqst_wk_dt
AND a.rcpnt_id = 'CLMNT'
LEFT OUTER JOIN ui_claim_pmt b
ON c.claimant_id = b.claimant_id
AND c.claim_id = b.claim_id
AND warrant_dt BETWEEN '1 June 2011' AND '30 June 2011'
AND b.status_cd = 'PAID'
AND a.rcpnt_id = b.rcpnt_id --AND A.PMT_NU = B.PMT_NU
LEFT OUTER JOIN ui_auth_pmt e
ON c.claimant_id = e.claimant_id
AND c.claim_id = e.claimant_id
AND c.rqst_wk_dt = e.rqst_wk_dt
AND d.mon_seq_nu = e.mon_seq_nu
WHERE c.rqst_wk_dt BETWEEN '1 June 2011' AND '30 June 2011'
AND d.bspd_type_cd = 'ALTR')
Above is the query which I have in Oracle and run in TOAD. Below is the query in LINQ. I have just done internal select statement and I wonder how to implement the select count and distinct for the query:
var enddate = Convert.ToDateTime("6/30/2011");
var Altquery = from D in UiMonHdr
join C in UiRqstWkCtrl on new {D.ClaimantId, D.ClaimId} equals new {C.ClaimantId, C.ClaimId}
join A in UiDstbPmt on new {C.ClaimantId, C.ClaimId , C.RqstWkDt} equals new {A.ClaimantId, A.ClaimId, A.RqstWkDt}
where A.RcpntId.Contains("CLMNT")
join B in UiClaimPmt on new {C.ClaimantId, C.ClaimId, A.RcpntId} equals new {B.ClaimantId, B.ClaimId, B.RcpntId}
where B.StatusCd.Contains("PAID") && B.WarrantDt >= startdate && B.WarrantDt <= enddate
join E in UiAuthPmt on new {C.ClaimantId, C.ClaimId , C.RqstWkDt} equals new {E.ClaimantId, E.ClaimId, E.RqstWkDt}
where C.RqstWkDt >= startdate && C.RqstWkDt <= enddate && D.BspdTypeCd.Contains("ALTR")
select new {ClaimantId = C.ClaimantId, ClaimId = C.ClaimId, PmtAmt = A.PmtAm, RqstWkDt = C.RqstWkDt}; Altquery.Dump();

LINQ count query returns a 1 instead of a 0

I have the following view:-
CREATE VIEW tbl_adjudicator_result_view
AS
SELECT a.adjudicator_id, sar.section_adjudicator_role_id, s.section_id, sdr.section_dance_role_id, d.dance_id, c.contact_id,
ro.round_id, r.result_id, c.title, c.first_name, c.last_name, d.name, r.value, ro.type
FROM tbl_adjudicator a
INNER JOIN tbl_section_adjudicator_role sar on sar.section_adjudicator_role2adjudicator = a.adjudicator_id
INNER JOIN tbl_section s on sar.section_adjudicator_role2section = s.section_id
INNER JOIN tbl_section_dance_role sdr on sdr.section_dance_role2section = s.section_id
INNER JOIN tbl_dance d on sdr.section_dance_role2dance = d.dance_id
INNER JOIN tbl_contact c on a.adjudicator2contact = c.contact_id
INNER JOIN tbl_round ro on ro.round2section = s.section_id
LEFT OUTER JOIN tbl_result r on r.result2adjudicator = a.adjudicator_id AND r.result2dance = d.dance_id
When I run the following query directly against the db I get 0 in the count column where there is no result
select adjudicator_id, first_name, COUNT(result_id)
from tbl_adjudicator_result_view arv
where arv.round_id = 16
group by adjudicator_id, first_name
However when I use LINQ query I always get 1 in the Count Column
var query = from arv in db.AdjudicatorResultViews
where arv.round_id == id
group arv by new { arv.adjudicator_id, arv.first_name} into grp
select new AdjudicatorResultViewGroupedByDance
{
AdjudicatorId = grp.Key.adjudicator_id,
FirstName = grp.Key.first_name,
Count = grp.Select(p => p.result_id).Distinct().Count()
};
What do I need to change in the View / Linq query.
You're not doing the same thing in the LINQ query as in the SQL. COUNT(result_id) does not count distinct values of result_id - it counts non-null values.
Try this instead:
Count = grp.Select(p => p.result_id).Where(x => x != null).Count()
The point is: you're grouping your data in the LINQ query - and you'll always get at least one group.
That group's Count may be 0 - but the count of groups will be 1.

Resources