Morning,
I'm running the following SELECT statement on a DB2 server (IBM Power System) and it returns the latest record from tableB based on a Timestamp (all good).
SELECT * FROM library1/tableA
JOIN library1/tableB on tableB.PRDCOD = tableA.NPROD
WHERE tableB.PRDCOD = '5520' and tableA.SPRTXT01 <> '0/9'
ORDER BY tableB.timstp DESC FETCH NEXT 1 ROWS ONLY
I now need to change this statement to update tableA and set field SPRTXT01 = '0/9', but only if tableB.SRVRSP= 'SUCCESSFUL' i.e. the latest record from tableB has a response of 'SUCCESSFUL'.
But I don't know how to format this statement correctly. Can anyone assist please?
I've tried the below, but this updated ALL rows in the table
UPDATE library1/tableA
SET tableA.SPRTXT01 = '0/9'
Where exists (
Select '1'
FROM library1/tableA
JOIN library1/tableB on
tableB.PRDCOD = tableA.NPROD
WHERE tableB.PRDCOD = '5520' and tableB.SRVRSP = 'SUCCESSFUL'
and tableA.SPRTXT01 <> '0/1'
ORDER BY tableB.timstp DESC FETCH NEXT 1 ROWS ONLY)
and I don't think it's applying the selection correctly i.e. rather than selecting the latest record from table B and then applying the RVSRP = 'SUCCESSFUL' check, it is only selecting the latest record for table B where SRVSRP = 'SUCCESSFUL'.
Thanks
Try this:
CREATE TABLE tableA (NPROD VARCHAR (10), SPRTXT01 VARCHAR (3));
CREATE TABLE tableB (PRDCOD VARCHAR (10), SRVRSP VARCHAR (20), timstp TIMESTAMP);
INSERT INTO tableA (NPROD, SPRTXT01)
VALUES ('5520', ''), ('XXXX', '');
INSERT INTO tableB (PRDCOD, SRVRSP, timstp)
VALUES
('5520', 'SUCCESSFUL', CURRENT TIMESTAMP)
, ('5520', 'UNSUCCESSFUL', CURRENT TIMESTAMP + 1 SECOND)
-- Comment out the next row to make it NOT update the SPRTXT01 column
, ('5520', 'SUCCESSFUL', CURRENT TIMESTAMP + 2 SECOND)
;
UPDATE tableA
SET tableA.SPRTXT01 = '0/9'
Where tableA.NPROD = '5520' AND tableA.SPRTXT01 <> '0/9'
AND exists
(
SELECT 1
FROM tableB
JOIN (SELECT PRDCOD, MAX (timstp) AS timstp FROM tableB GROUP BY PRDCOD) G
ON (G.PRDCOD, G.timstp) = (tableB.PRDCOD, tableB.timstp)
WHERE tableB.PRDCOD = tableA.NPROD
AND tableB.SRVRSP = 'SUCCESSFUL'
);
SELECT * FROM tableA;
NPROD
SPRTXT01
5520
0/9
XXXX
fiddle
I have two tables, headers and lines. I need to grab the batch_submission_date from the header table, but sometimes a query for batch_id will return a null for batch_submission_date, but will also return a parent_batch_id, and if we query THAT parent_batch_id as a batch_id, it will then return the correct batch_submission_date.
e.g.
SELECT t1.batch_id,
t1.parent_batch_id,
t2.batch_submission_date
FROM db.headers t1, db.lines t2
WHERE t1.batch_id = '12345';
output = 12345, 99999, null
Then we use that parent batch_id as a batch_id :
SELECT t1.batch_id,
t1.parent_batch_id,
t2.batch_submission_date
FROM db.headers t1, db.lines t2
WHERE t1.batch_id = '99999';
and we get output = 99999,99999,'2018-01-01'
So I'm trying to write a query that will do this for me - anytime a batch_id's batch_submission_date is null, we find that batch_id's parent batch_id and query that instead.
This was my idea - but I just get back null both for bp_batch_submission_date and for new_submission_date.
SELECT
t1.parent_id as parent_id,
t1.BATCH_ID as bp_batch_id,
t2.BATCH_LINE_NUMBER as bp_batch_li,
t1.BATCH_SUBMISSION_DATE as bp_batch_submission_date,
CASE
WHEN t1.BATCH_SUBMISSION_DATE is null
THEN
(SELECT a.BATCH_SUBMISSION_DATE
FROM
db.headers a,
db.lines b
WHERE
a.SD_BATCH_HEADERS_SKEY = b.SD_BATCH_HEADERS_SKEY
and a.parent_batch_id = bp_batch_id
and b.batch_line_number = bp_batch_li
) END as new_submission_date
FROM
db.headers t1,
db.lines t2
WHERE
t1.SD_BATCH_HEADERS_SKEY = t2.SD_BATCH_HEADERS_SKEY
and (t1.BATCH_ID = '12345' or t1.PARENT_BATCH_ID = '12345')
and t2.BATCH_LINE_NUMBER = '1'
GROUP BY
t2.BATCH_CLAIM_LINE_STATUS_DESC,
t1.PARENT_BATCH_ID,
t1.BATCH_ID,
t2.BATCH_LINE_NUMBER,
t1.BATCH_SUBMISSION_DATE;
is what I'm trying to do possible? using the bp_batch_id and bp_batch_li variables
Use CTE (common table expression) to avoid redundant code, then use coalesce() to find parent date in case of null. In your first queries you didn't attach joining condition between two tables, I assumed it's based on sd_batch_headers_skey like in last query.
dbfiddle demo
with t as (
select h.batch_id, h.parent_batch_id, l.batch_submission_date bs_date
from headers h
join lines l on l.sd_batch_headers_skey = h.sd_batch_headers_skey
and l.batch_line_number = '1' )
select batch_id, parent_batch_id,
coalesce(bs_date, (select bs_date from t x where x.batch_id = t.parent_batch_id)) bs_date
from t
where batch_id = 12345;
You could use simpler syntax with connect by and level <= 2 but if in your data there are really rows containing same ids (99999, 99999) then we get cycle error.
I need two combine two queries to get one result only. Here's the first code.
SELECT FKLTI_KTRGN, COUNT(DISTINCT VIEW_PELAJAR_ENROLL.MB_NAMA) AS BIL_UG
FROM VIEW_PELAJAR_ENROLL, KOD_BANGSA, KOD_NEGERI, JANTINA, KOD_AGAMA, KOD_CACAT, TARAF_KAHWIN
WHERE KOD_KTRGN_PROGRAM = 'SARJANA MUDA'
AND MS_SESI = '2015/2016'
AND MB_BANGSA = KOD_BANGSA
AND KOD_NEGERI = MB_ASAL
AND MB_JANTINA = JAN_KOD
AND KOD_AGAMA = MB_AGAMA
AND KOD_CACAT = MB_CACAT
AND KOD_TARAF = MB_TARAF_KAHWIN
AND MS_STATUS_SEMASA IN
(SELECT SP_KOD
FROM STATUS_PELAJAR
WHERE STATUS_ENROLL = 'Y'
AND SP_TAMAT IS NULL
AND SP_KOD = MS_STATUS_SEMASA)
GROUP BY FKLTI_KTRGN
And this is the second code.
SELECT FKLTI_KTRGN, COUNT(DISTINCT PELAJAR_BIODATA.MBUT_NAMA) AS BIL_PG
FROM PELAJAR_BIODATA,KOD_NEGERI,FAKULTI,KOD_IJAZAH,PELAJAR_BUTIR_PENGAJIAN,STATUS_PENGAJIAN A,
KOD_STATUS_PELAJAR_IPS, KOD_CACAT, KOD_BANGSA, E_KOD_PENGAJIAN, JANTINA, KOD_PROGRAM
WHERE A.STAT_STATUS IN (SELECT KOD_STATUS
FROM KOD_STATUS_PELAJAR_IPS
WHERE STATUS_ENROLL='Y'
AND KOD_STATUS=STAT_STATUS)
AND A.STAT_NOMKPB=MBUT_NOMKPB
AND PBP_PROGRAM IN ('5','6')
AND MBUT_ASAL=KOD_NEGERI(+)
AND SUBSTR(A.STAT_KOD_IJAZAH,1,1)=FKLTI_KOD
AND PBP_PROGRAM=KOD_PROGRAM.KOD_PROGRAM
AND A.STAT_KOD_IJAZAH=KOD_IJAZAH_UM
AND A.STAT_NODAFTAR=PBP_NODAFTAR
AND MBUT_CACAT=KOD_CACAT
AND KOD_BANGSA=MBUT_BANGSA
AND PBP_JENIS_PENGAJIAN=KOD_JNS_PENGAJIAN
AND MBUT_JANTINA=JAN_KOD
AND MBUT_WARGA IS NOT NULL
AND MBUT_BANGSA IS NOT NULL
AND MBUT_JANTINA IS NOT NULL
AND MBUT_NEGERI IS NOT NULL
AND PBP_PROGRAM IS NOT NULL
AND A.STAT_STATUS=KOD_STATUS
AND PBP_KOD_IJAZAH IS NOT NULL
AND A.STAT_SESI||A.STAT_SEMESTER IN (SELECT MAX(B.STAT_SESI||B.STAT_SEMESTER) FROM STATUS_PENGAJIAN B
WHERE B.STAT_NODAFTAR=A.STAT_NODAFTAR
AND B.STAT_NOMKPB=A.STAT_NOMKPB
AND A.STAT_SESI||A.STAT_SEMESTER=B.STAT_SESI||B.STAT_SEMESTER)
AND A.STAT_SESI = '2015/2016'
group by FKLTI_KTRGN
This is the expected result:
This is what i get:
I've tried to use UNION but it doesn't work. Anyone can help me? I really need your help. Thank you in advance!
In order to use union, you need to have the same number of fields and the same field name of both query.
as in both queries of yours, the second field has different name: BIL_PG, BIL_UG
Try this
select * from
(SELECT FKLTI_KTRGN, COUNT(DISTINCT VIEW_PELAJAR_ENROLL.MB_NAMA) AS BIL_UG
FROM VIEW_PELAJAR_ENROLL, KOD_BANGSA, KOD_NEGERI, JANTINA, KOD_AGAMA, KOD_CACAT, TARAF_KAHWIN
WHERE KOD_KTRGN_PROGRAM = 'SARJANA MUDA'
AND MS_SESI = '2015/2016'
AND MB_BANGSA = KOD_BANGSA
AND KOD_NEGERI = MB_ASAL
AND MB_JANTINA = JAN_KOD
AND KOD_AGAMA = MB_AGAMA
AND KOD_CACAT = MB_CACAT
AND KOD_TARAF = MB_TARAF_KAHWIN
AND MS_STATUS_SEMASA IN
(SELECT SP_KOD
FROM STATUS_PELAJAR
WHERE STATUS_ENROLL = 'Y'
AND SP_TAMAT IS NULL
AND SP_KOD = MS_STATUS_SEMASA)
GROUP BY FKLTI_KTRGN)
union
select * from (SELECT FKLTI_KTRGN, COUNT(DISTINCT PELAJAR_BIODATA.MBUT_NAMA) AS BIL_PG
FROM PELAJAR_BIODATA,KOD_NEGERI,FAKULTI,KOD_IJAZAH,PELAJAR_BUTIR_PENGAJIAN,STATUS_PENGAJIAN A,
KOD_STATUS_PELAJAR_IPS, KOD_CACAT, KOD_BANGSA, E_KOD_PENGAJIAN, JANTINA, KOD_PROGRAM
WHERE A.STAT_STATUS IN (SELECT KOD_STATUS
FROM KOD_STATUS_PELAJAR_IPS
WHERE STATUS_ENROLL='Y'
AND KOD_STATUS=STAT_STATUS)
AND A.STAT_NOMKPB=MBUT_NOMKPB
AND PBP_PROGRAM IN ('5','6')
AND MBUT_ASAL=KOD_NEGERI(+)
AND SUBSTR(A.STAT_KOD_IJAZAH,1,1)=FKLTI_KOD
AND PBP_PROGRAM=KOD_PROGRAM.KOD_PROGRAM
AND A.STAT_KOD_IJAZAH=KOD_IJAZAH_UM
AND A.STAT_NODAFTAR=PBP_NODAFTAR
AND MBUT_CACAT=KOD_CACAT
AND KOD_BANGSA=MBUT_BANGSA
AND PBP_JENIS_PENGAJIAN=KOD_JNS_PENGAJIAN
AND MBUT_JANTINA=JAN_KOD
AND MBUT_WARGA IS NOT NULL
AND MBUT_BANGSA IS NOT NULL
AND MBUT_JANTINA IS NOT NULL
AND MBUT_NEGERI IS NOT NULL
AND PBP_PROGRAM IS NOT NULL
AND A.STAT_STATUS=KOD_STATUS
AND PBP_KOD_IJAZAH IS NOT NULL
AND A.STAT_SESI||A.STAT_SEMESTER IN (SELECT MAX(B.STAT_SESI||B.STAT_SEMESTER) FROM STATUS_PENGAJIAN B
WHERE B.STAT_NODAFTAR=A.STAT_NODAFTAR
AND B.STAT_NOMKPB=A.STAT_NOMKPB
AND A.STAT_SESI||A.STAT_SEMESTER=B.STAT_SESI||B.STAT_SEMESTER)
AND A.STAT_SESI = '2015/2016'
group by FKLTI_KTRGN)
I have a junction table in a (SQL Server 2014) database with columns FirstID and SecondID. Given a specific FirstID, I'd like to find all other FirstIDs from the table that have an equivalent set of SecondIDs (even if that set is empty).
Sample Data:
FirstId SecondId
1 1
1 2
2 3
3 1
3 2
... ...
In the case of the sample data, if I specified FirstID = 1, then I'd expect 3 to appear in the result set.
I've tried the following so far, which works pretty well except for empty sets:
SELECT FirstSecondEqualSet.FirstId
FROM FirstSecond FirstSecondOriginal
INNER JOIN FirstSecond FirstSecondEqualSet ON FirstSecondOriginal.SecondId = FirstSecondEqualSet.SecondId
WHERE FirstSecondOriginal.FirstId = #FirstId
AND FirstSecondEqualSet.FirstId != #FirstId
GROUP BY FirstSecondEqualSet.FirstId
HAVING COUNT(1) = (SELECT COUNT(1) FROM FirstSecond WHERE FirstSecond.FirstId = #FirstId)
I think it's somehow related to Relational Division with no Remainder (RDNR). See this great article by Dwain Camps for reference.
DECLARE #firstId INT = 1
SELECT
f2.FirstId
FROM FirstSecond f1
INNER JOIN FirstSecond f2
ON f2.SecondId = f1.SecondId
AND f1.FirstId <> f2.FirstId
WHERE
f1.FirstId = #firstId
GROUP BY f2.FirstId
HAVING COUNT(*) = (SELECT COUNT(*) FROM FirstSecond WHERE FirstId = #firstId)
Here is one approach. It counts the number of values for each firstid and then joins on the secondid.
select fs2.firstid
from (select fs1.*, count(*) over (partition by firstid) as numseconds
from firstsecond fs1
where fs1.firstid = #firstid
) fs1 join
(select fs2.*, count(*) over (partition by firstid) as numseconds
from firstsecond fs2
) fs2
on fs1.secondid = fs2.secondid and fs1.numseconds = fs2.numseconds
group by fs2.firstid
having count(*) = max(fs1.numseconds);
I'm getting the error [ORA-01427: single-row subquery returns more than one row] when I execute a query. I have a query structured like so:
SELECT LV.PRICE,
(SELECT C.MODEL_NAME FROM CARS C WHERE C.MODEL_ID = LV.MODEL_ID) as MODEL_NAME
FROM LEDGER_VIEW LV
WHERE LV.PRICE < 500
It's breaking on the nested select. I know the logic both in the view and in this query is correct, and that there's no chance of the nested select returning more than one row. The CARS table's MODEL_ID is a unique field. If I execute the query without the nested select it doesn't return this error.
The LEDGER_VIEW is a view built on top of another view. Is it possible that these stacked views are buggy in Oracle 10g? I don't know how else to debug this problem.
I am aware I could change this particular query to a join rather than a nested select, but I'd like to know why this is happening because I use nested queries in other places where it is not so easily modifiable.
EDIT: Here's the really strange thing. The LEDGER_VIEW is, as I said, built on top of another view. As a test, I copied the nested view's SQL directly into the SQL of the SQL of LEDGER_VIEW, in place of the nested view, and it returned with no errors (as expected). This seems to confirm to me that there is some buggy behavior either with nested views or with the combination of nested views + database links.
Your subquery is returning multiple rows. Use the query below to find out which MODELID values in the Car table are duplicated:
select MODELID as CarsModelID, count(*) as Count
from cars
where MODELID in (
select MODEL_ID
from LEDGER_VIEW
WHERE LV.PRICE < 500
)
group by MODELID
having count(*) > 1
I am unable to recreate via a creation of a stacked view. (althoug RedFilters will find the culprit)
CREATE TABLE t1
(
t1_id NUMBER ,
txt VARCHAR2( 50 ),
CONSTRAINT t1_pk PRIMARY KEY( t1_id )
) ;
CREATE TABLE t2
(
t2_id NUMBER ,
t1_id NUMBER ,
price NUMBER( 10, 4 ) ,
CONSTRAINT t2_pk PRIMARY KEY( t2_id ),
CONSTRAINT t2_fk FOREIGN KEY( t1_id ) REFERENCES t1( t1_id )
);
insert into t1(t1_id, txt) values(1,'fit');
insert into t1(t1_id, txt) values(2,'focus');
insert into t1(t1_id, txt) values(3,'golf');
insert into t1(t1_id, txt) values(4,'explorer');
insert into t1(t1_id, txt) values(5,'corolla');
insert into t2(t2_id, t1_id, price) values(1,1,17000);
insert into t2(t2_id, t1_id, price) values(2,2,16000);
insert into t2(t2_id, t1_id, price) values(3,3,22000);
insert into t2(t2_id, t1_id, price) values(4,4,31000);
insert into t2(t2_id, t1_id, price) values(5,5,17000);
create view t1_view as select * from t1;
create view t2_view as select * from t2;
create view t_stacked_view as
select t1_view.txt ,
t2_view.price ,
t1_view.t1_id
from t1_view
left join
t2_view
on t1_view.t1_id = t2_view .t1_id
;
--stacked view test
select t1_view.txt ,
(select t_stacked_view.price
from t_stacked_view
where t1_view.t1_id = t_stacked_view .t1_id) price
from t1_view ;
--or better yet, just drop the row level query
select t1_view.txt ,
t2_view.price
from t1_view
left join
t2_view
on t1_view.t1_id = t2_view .t1_id
;
But that begs the question, why are you doing the row level query here? While 10g ought to optimize them the same, I have always found it easier to write queries as below, both for readability, maintainability, and to specifically avoid the error you are having (is it always, 3 years down the road, guaranteed by the application (both in the db and the calling app) that you cannot have a condition that will cause this error? One rouge statement gets in and your entire app dies?
SELECT LV.PRICE,
c.model_name
FROM LEDGER_VIEW LV
LEFT /* OR INNER */ JOIN CARS C
ON C.MODEL_ID = LV.MODEL_ID
WHERE LV.PRICE < 500
I suggest using RedFilter's answer to check whether there are multiple cars with a given MODEL_ID.
If you're absolutely certain that CARS.MODEL_ID is unique, then it implies that the error message is generated by selection from LEDGER_VIEW - so try running the equivalent query without the subquery on CARS, like so:
SELECT LV.PRICE
FROM LEDGER_VIEW LV
WHERE LV.PRICE < 500
If you still see the same error (you should, if CARS.MODEL_ID is unique) you will need to debug LEDGER_VIEW - ie. check for sub-queries returning multiple rows in LEDGER_VIEW and the underlying views it is based on.
Creating views based on views is possible in most forms of SQL, but it is usually a bad idea - for this very reason.
Try forcing your subquery to return a single result by appending rownum = 1, like this:
SELECT LV.PRICE,
(SELECT C.MODEL_NAME FROM CARS C WHERE C.MODEL_ID = LV.MODEL_ID AND ROWNUM = 1) as MODEL_NAME
FROM LEDGER_VIEW LV
WHERE LV.PRICE < 500
It will probably work and if it does, you will know that your subquery returns multiple rows, which judging by the error code it should be. Of course this is not a solution so you might have to fix your data in cars table to actually solve the problem. Leaving and rownum = 1 will eliminate the error if model_id is duplicated again, preventing you from noticing the problem.
select
a.account_number,
a.party_id,
a.TRX_NUMBER,
a.trx_date,
a.order_number,
adv.unapplied_amt,
a.Finance,
a.customer_name,a.PARTY_NAME,
a.customer_number,a.contact_number,
a.name,
a.Aging,
a.transaction_type,
a.exec_name,
a.team_leader,
sum(a.O_SAmount),
(case when (trunc(sysdate) - trunc(a.trx_date)) <=:ag1 then sum(a.O_SAmount) else 0 end ) bucket1,--"<" || :ag1,
(case when (trunc(sysdate) - trunc(a.trx_date)) between :ag1+1 and :ag2 then sum(a.O_SAmount) else 0 end ) bucket2,--:ag1+1 || "to" || :ag2,
(case when (trunc(sysdate) - trunc(a.trx_date)) between :ag2+1 and :ag3 then sum(a.O_SAmount) else 0 end ) bucket3,--:ag2+1 || "to" || :ag3,
(case when (trunc(sysdate) - trunc(a.trx_date)) >:ag3 then sum(a.O_SAmount) else 0 end ) bucket4,
:AS_ON_date
from
(select distinct hca.account_number,hp.party_id,--rcta.CUSTOMER_TRX_ID,
--rcta.trx_number,rcta.trx_date,apsa.due_date,
(
select distinct
--ooha.order_number,
rcta.trx_number
--to_char(rcta.trx_date,'DD-MON-YYYY') trx_date
from
ra_customer_trx_all rcta,
oe_order_headers_all ooh,
oe_order_lines_all oola,
--ra_customer_trx_all rcta,
ra_customer_trx_lines_all rctla,
ra_cust_trx_types_all rctta
--ra_customer_trx_lines_all rctl
where 1=1
AND ooh.header_id = oola.header_id
--AND ooh.order_number = '111111010101698'
AND ooh.order_number=oohA.order_number
AND TO_CHAR (ooh.order_number) = rcta.ct_reference
AND rcta.customer_trx_id = rctla.customer_trx_id
AND rctla.inventory_item_id = oola.inventory_item_id
and rcta.CUST_TRX_TYPE_ID = rctta.cust_trx_type_id
and rcta.org_id = rctta.org_id
and rctta.type like 'INV'
and oola.ordered_item LIKE 'MV%'
AND oola.attribute3 = 'Y'
AND ooh.flow_status_code <> 'ENTERED'
AND oola.flow_status_code <> 'CANCELLED'
)TRX_NUMBER,
(select distinct
--ooha.order_number,
--rcta.trx_number
rcta.trx_date
from
ra_customer_trx_all rcta,
oe_order_headers_all ooh,
oe_order_lines_all oola,
--ra_customer_trx_all rcta,
ra_customer_trx_lines_all rctla,
ra_cust_trx_types_all rctta
--ra_customer_trx_lines_all rctl
where 1=1
AND ooh.header_id = oola.header_id
--AND ooh.order_number = '111111010101698'
AND ooh.order_number=oohA.order_number
AND TO_CHAR (ooh.order_number) = rcta.ct_reference
AND rcta.customer_trx_id = rctla.customer_trx_id
AND rctla.inventory_item_id = oola.inventory_item_id
and rcta.CUST_TRX_TYPE_ID = rctta.cust_trx_type_id
and rcta.org_id = rctta.org_id
and rctta.type like 'INV'
and oola.ordered_item LIKE 'MV%'
AND oola.attribute3 = 'Y'
AND ooh.flow_status_code <> 'ENTERED'
AND oola.flow_status_code <> 'CANCELLED'
)TRX_Date,
rcta.INTERFACE_HEADER_ATTRIBUTE1 order_number,
ooha.attribute10 Finance,
f.customer_name,HP.PARTY_NAME,
TO_NUMBER(f.customer_number)customer_number,hp.primary_phone_number contact_number,--csi.incident_number,
--cii.instance_number,
haou.name,
--sum(acr.amount) Advance,--rcta.CUST_TRX_TYPE_ID,--acr.cash_receipt_id,
--sum(abs((apsa.AMOUNT_DUE_REMAINING-nvl(acr.amount,0)))) "O_SAmount",
apsa.AMOUNT_DUE_REMAINING O_SAmount,
--sum(abs((apsa.AMOUNT_DUE_REMAINING))) "O_SAmount",
round(months_between(sysdate,rcta.trx_date)*30) Aging,
--(case when ((round(months_between(sysdate,rcta.trx_date)*30)>=0) or (round(months_between(sysdate,rcta.trx_date)*30)<:aging1)) then apsa.AMOUNT_DUE_REMAINING end) "0 TO 30"
--(case when (trunc(sysdate) - trunc(apsa.Due_Date)) <=:ag1 then apsa.AMOUNT_DUE_REMAINING else 0 end ) bucket1,--"<" || :ag1,
--(case when (trunc(sysdate) - trunc(apsa.Due_Date)) between :ag1+1 and :ag2 then apsa.AMOUNT_DUE_REMAINING else 0 end ) bucket2,--:ag1+1 || "to" || :ag2,
--(case when (trunc(sysdate) - trunc(apsa.Due_Date)) between :ag2+1 and :ag3 then apsa.AMOUNT_DUE_REMAINING else 0 end ) bucket3,--:ag2+1 || "to" || :ag3,
--(case when (trunc(sysdate) - trunc(apsa.Due_Date)) >:ag3 then apsa.AMOUNT_DUE_REMAINING else 0 end ) bucket4,
--apsa.amount_due_original,
--TO_NUMBER(apsa.AMOUNT_DUE_REMAINING)AMOUNT_DUE_REMAINING,
rctta.name transaction_type,
PAPF.full_name||'-'||PAPF.EMPLOYEE_NUMBER exec_name,
ooha.attribute9 team_leader,
:AS_ON_date
from ra_customer_trx_all rcta,
oe_order_headers_all ooha,
hz_cust_accounts hca,
hz_parties hp,
--cs_incidents_all_b csi,
--csi_item_instances cii,
hr_all_organization_units haou,
ar_cash_receipts_all acr,
ar_receivable_applications_all aaa,
ra_cust_trx_types_all RCTTA,
hr.per_all_people_f papf,
ar_customers f,
ar_payment_schedules_all apsa,
jtf.JTF_RS_SALESREPS jrs
where 1=1
--and INTERFACE_HEADER_ATTRIBUTE1 like '111111060100538'
--and INTERFACE_HEADER_ATTRIBUTE1 like '111111010105402'
--and INTERFACE_HEADER_ATTRIBUTE1 like '111111010102791'
and rcta.ct_reference(+)=TO_CHAR(ooha.order_number)
AND f.customer_id = (rcta.bill_to_customer_id)
and f.customer_id=hca.cust_account_id
and hca.party_id=hp.party_id
and haou.organization_id=rcta.INTERFACE_HEADER_ATTRIBUTE10
--and hp.party_id=cii.owner_party_id
--and csi.inventory_item_id=cii.inventory_item_id
--and csi.inv_organization_id=haou.organization_id
--and haou.organization_id=nvl(:location,haou.organization_id)
and ooha.SHIP_FROM_ORG_ID=nvl(:location,haou.organization_id)
AND RCTTA.NAME like :transaction_type||'%'
--decode(:org_id,null,null,(select name from ar_cash_receipts_all where organization_id = :org_id)) ||'%')
and rcta.trx_date<=to_date(:AS_ON_date)
--AND RCTTA.NAME=NVL(:TRANS_TYPE,RCTTA.NAME)
and rcta.org_id=nvl(:org_id,rcta.org_id)
--and f.customer_name like 'VIKAS SATAV'
and aaa.applied_customer_trx_id(+)=rcta.customer_trx_id
and aaa.cash_receipt_id=acr.cash_receipt_id(+)
and rcta.status_trx like 'OP'
and rcta.CUST_TRX_TYPE_ID=rctta.CUST_TRX_TYPE_ID
and apsa.CUSTOMER_TRX_ID=rcta.CUSTOMER_TRX_ID
and TO_NUMBER(apsa.AMOUNT_DUE_REMAINING) >0
--and hp.party_id=papf.party_id(+)
and jrs.salesrep_id = ooha.SALESREP_ID
and jrs.ORG_ID = ooha.ORG_ID
and jrs.PERSON_ID = papf.PERSON_ID(+)
) a,
(
select
b.order_number,
sum(b.AMOUNT_APPLIED) unapplied_amt
from
(select distinct to_char(ooha.order_number) order_number,ara.* from
oe_order_headers_all ooha,
oe_payments oe,
ar_receivable_applications_all ara
where 1=1--ooha.order_number = :p_order_num
and oe.header_id=ooha.header_id
and ara.PAYMENT_SET_ID=oe.PAYMENT_SET_ID
and ara.DISPLAY='Y'
and (ara.STATUS like 'OTHER ACC' or ara.STATUS like 'UNAPP') --or ara.STATUS like 'ACC')
) b
group by b.order_number
) adv
where adv.order_number(+)=a.order_number
group by a.account_number,
a.party_id,
a.TRX_NUMBER,
a.trx_date,
a.order_number,
adv.unapplied_amt,
a.Finance,
a.customer_name,a.PARTY_NAME,
a.customer_number,a.contact_number,
a.name,
a.Aging,
a.transaction_type,
a.exec_name,
a.team_leader
order by a.Aging desc