Help in SQL to balance the values ... ?

From: Tareq Salem <nova1427_at_gmail.com>
Date: Sat, 22 Dec 2012 01:00:31 -0800 (PST)
Message-ID: <28147be6-c7e7-4b56-9554-d4aef0c6bf53_at_googlegroups.com>



Hi,  

I have these tables, I would like to make balance the values and distribute the amount between links.    

CREATE TABLE F_LINKTO_M
(
  F CHAR(2),
  M CHAR(2)
);  

CREATE TABLE F_V
(
  F CHAR(2),
  F_V NUMBER
);  

CREATE TABLE M_V
(
  M CHAR(2),
  M_V NUMBER
);  

INSERT INTO F_LINKTO_M VALUES ('F1','M1');
INSERT INTO F_LINKTO_M VALUES ('F1','M2');
INSERT INTO F_LINKTO_M VALUES ('F1','M3');
INSERT INTO F_LINKTO_M VALUES ('F2','M1');
INSERT INTO F_LINKTO_M VALUES ('F2','M2');
INSERT INTO F_LINKTO_M VALUES ('F3','M2');
 
INSERT INTO F_V VALUES ('F1', 70);

INSERT INTO F_V VALUES ('F2', 80);
INSERT INTO F_V VALUES ('F3', 100);  
INSERT INTO M_V VALUES ('M1', 20);
INSERT INTO M_V VALUES ('M2', 210);
INSERT INTO M_V VALUES ('M3', 30);
 

COMMIT;     Step 1
I will start with F which has minimum link to M (that means F3) I make minus minimum value between them (F1=100, M2=210) that means minus 100 for both on link between F and M F F_V



F1 70
F2 80
F3 0  

M M_V



M1 20
M2 110
M3 30    

Step 2
I will start with F which has minimum link to M (that means F2) I make minus minimum value between them (F2=80, M1=20) that means minus is 20 for both on link between F and M F F_V



F1 70
F2 60
F3 0  

M M_V



M1 0
M2 110
M3 30  

Still I can make balance because F2 has another like with M3. I make minus minimum value between them (F2=60, M2=110) that means minus is 60 for both on link between F and M F F_V



F1 70
F2 0
F3 0  

M M_V



M1 0
M2 50
M3 30  

Step 3
F1 is last that has value
I make minus minimum value between them (F1=70, M2=50) that means minus is 50 for both on link between F and M F F_V



F1 20
F2 0
F3 0  

M M_V



M1 0
M2 0
M3 30  

Still I can make balance because F1 has another like with M3. I make minus minimum value between them (F1=20, M=30) that means minus is 20 for both on link between F and M F F_V



F1 0
F2 0
F3 0  

M M_V



M1 0
M2 0
M3 10      

How can I do got the final result in a query?  

Regards; Received on Sat Dec 22 2012 - 10:00:31 CET

Original text of this message