chokka
asked on
Complex SQL Query with Cursor
I have to write a SQL Stored Procedure by passing DOC Id as Input parameter and has to calculate the fee for each parent doc.
In my Document Table, I have Original Doc and Child Doc. For every doc, I pass as an Input parameter for my stored procedure, I have to find whether the Doc is a Child Doc or Parent Doc and if it is child, I have update the fee as $0.40.
Based on my example,
On passing AB12456 it has only one child and so billing is $0.40.
On passing CY12345, It has 3 child and one of its child became parent with two children. And one of its grandchild became parent and has 2 children. So this Parent Doc should be billed as $2.80.
Original Doc Child Doc Fee Expected Fee
AB12456 NX12450 $0.40
CY12345 NX23410
CY12345 NX23421
CY12345 NX23432
NX23410 NY23411
NX23410 NY23422
NY23422 NZ23411
NY23422 NZ23422 $2.80
I have to write a logic without hard coding and calculate, how much each parent doc is billed. Amount has to be updated in the Fee Column.
How should i do this?
In my Document Table, I have Original Doc and Child Doc. For every doc, I pass as an Input parameter for my stored procedure, I have to find whether the Doc is a Child Doc or Parent Doc and if it is child, I have update the fee as $0.40.
Based on my example,
On passing AB12456 it has only one child and so billing is $0.40.
On passing CY12345, It has 3 child and one of its child became parent with two children. And one of its grandchild became parent and has 2 children. So this Parent Doc should be billed as $2.80.
Original Doc Child Doc Fee Expected Fee
AB12456 NX12450 $0.40
CY12345 NX23410
CY12345 NX23421
CY12345 NX23432
NX23410 NY23411
NX23410 NY23422
NY23422 NZ23411
NY23422 NZ23422 $2.80
I have to write a logic without hard coding and calculate, how much each parent doc is billed. Amount has to be updated in the Fee Column.
How should i do this?
ASKER
@ste5an .. Great Help !! But the output is incorrect.. Hierarchy is great syntax .. which i wasn't aware.
In the Below Insert Query , We have 3 columns - Original Doc, Child Doc and Fee. Fee is the column we have to set it.
Doc Id : AB12456 has one child doc ... So it has a Fee of $0.40
and Doc Id : CY12345 has 7 chidren ( Including sub-children ).. so it has a fee of $2.80
We are setting the fee for one child as $0.40. Based on that we calculate the children fee.
In the Below Insert Query , We have 3 columns - Original Doc, Child Doc and Fee. Fee is the column we have to set it.
Doc Id : AB12456 has one child doc ... So it has a Fee of $0.40
and Doc Id : CY12345 has 7 chidren ( Including sub-children ).. so it has a fee of $2.80
We are setting the fee for one child as $0.40. Based on that we calculate the children fee.
INSERT INTO @Sample
VALUES ( 'AB12456', 'NX12450', NULL ),
( 'CY12345', 'NX23410', NULL ),
( 'CY12345', 'NX23421', NULL ),
( 'CY12345', 'NX23432', NULL ),
( 'NX23410', 'NY23411', NULL ),
( 'NX23410', 'NY23422', NULL ),
( 'NY23422', 'NZ23411', NULL ),
( 'NY23422', 'NZ23422',NULL);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@ste5an, I am so thankful.
I got an opportunity to learn the Hierarchy Syntax. I am aware about Hierarchy Connect By Syntax in Oracle.
This is so great.
Thanks
I got an opportunity to learn the Hierarchy Syntax. I am aware about Hierarchy Connect By Syntax in Oracle.
This is so great.
Thanks
ASKER
Incredible !!
Open in new window