7
Answers

Update nested layer using loop #Challenge

Gautam Parmar

Gautam Parmar

7y
192
1
I have MasterPart 1 which is the price of Rs-100 Now if I will change the price of 100 it suppose to change the price of all effected layer as you can see in below image.
 
it supposed to change the price of  all MasterPart 2 to 5 based on the price of Master Part 1
 
Than It supposed to change price of all 3rd level master part based on the second level
 
And it not defined how much inner it will be. 
 
 
Answers (7)
1
Dharmraj Thakur

Dharmraj Thakur

NA 4.1k 61.7k 7y
1. I have created a table with data as you provide.
 
RowMaterialMaster

ID Name Price HasParent
1 M1 500 0
2 M2 750 0
3 M9 500 0
4 M57 1750 0
5 Master Part 1 0 1
6 Master Part 2 0 1
7 Master Part 3 0 1
8 Master Part 4 0 1
9 Master Part 5 0 1
10 Master Part 6 0 1
11 Master Part 7 0 1
 
RowMaterialMapping
RowMaterialID ParentRowMaterialID
5 1
5 2
6 3
6 5
10 4
10 6
 

2. Now take row materials which have no parent means which is not dependent on another materials like M1, M2, M9 etc...

3. Create a method in c# which will update price of all the row materials based on its 0 level child materials...

this method will take all rows () and
  1. var ids = GetAllIDs("select ID from RowMaterialMaster where HasParent = 1");  
  2. //first row it will be 5 
  1. //foreach() loop starts here
  2. declare @price as decimal(18,0);  
  3.   
  4. select @price += ParentRowMaterialID from RowMaterialMapping map  
  5. INNER JOIN RowMaterialMaster part ON part.ID = map.ParentRowMaterialID  
  6. where map.RowMaterialID = ids[i]; //value of ids[i] is 5  
  7.   
  8. select @price as Price; // here you will get total calculated price of given PartID  
  9.   
  10. //now update the price of specified ids[i] with @price  
  11. update RowMaterialMaster set Price = @price where ID = ids[i];  
  12. //loop ended 

4. you should call evertime Step 3 whenever price update of independent materials. Then you will get up to date data as expected...
 
That it. Try to understand my suggetion and I hope above rough logic will helpful for you. Please Ignore syntax and put exact logic as per your understanding...
 
 
0
Dharmraj Thakur

Dharmraj Thakur

NA 4.1k 61.7k 7y
Ohkay I got the difference... then It will fine by suggested structure... You can obviously loop over. Did you implete that or any other logic then show me when you are stucked?
0
Gautam Parmar

Gautam Parmar

NA 872 2.2k 7y
Difference between M1 and Master Part1 is
 
Some time Master Part1 could be a raw material for any part or it could be a final product as well.
 
And M1 is raw material which is not final product.
 
Example If we manufacture Electric Switch Board, Electric Switch board is final product
To make it we need switches.
 
And if we change the price of light inside used into switches we can say it is M1. so the all price will get affected which are uses M1.
 
 
0
Dharmraj Thakur

Dharmraj Thakur

NA 4.1k 61.7k 7y
Ohkay, I am little bit confuse that what is difference between Master Part 1 and M1? Is it not possible to store them together in same table?
0
Gautam Parmar

Gautam Parmar

NA 872 2.2k 7y
Hi Dharmaraj,
 
Thanks for your effort,
 
Problem i am facing that how to loop all nested layer
 
i.e. if i will update Master Part 1 how i will reach to Master Part 7.
 
And i cannot loop all raw material or part due to it has more than 1lc parts or material.
0
Gautam Parmar

Gautam Parmar

NA 872 2.2k 7y
Here is the detailed graph 
 
There is Master Part 1 and to create it two raw materials are required 
M1 -    500 Rs.
M2 -    750 Rs.
 
If we change the price of raw materials of M1 500 Rs to 600 Rs. so the cost of master part 1 is now 1350. 
 
And in hierarchy, as you can see to create Master part 2 as a raw material used as master part 1 - so that price of master part 2 will also get affected.
 
and to create Master part 6 as raw material Master part 2 is used to it will also get affect and it is so on nth number it is not defined how much deep it could be. 
 
It suppose to update all price of raw material which is in hierarchy
 
0
Dharmraj Thakur

Dharmraj Thakur

NA 4.1k 61.7k 7y
It means all nested childs will have dependent price of parent. Please clarify that If Master Par 1 having 100 Rs then how much of part 2 to 5?