Home » SQL & PL/SQL » SQL & PL/SQL » Get value between two fields (Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod, WINDOWS 2003)
Get value between two fields [message #611336] Tue, 01 April 2014 12:29 Go to next message
hissam78
Messages: 193
Registered: August 2011
Location: PAKISTAN
Senior Member
DEAR ALL,

Please help me to make a query

In Table-1 (UTL_CHARGES) we have start_mrl, end_mrl and Value Column Names with Following Data


LINE_ID	START_MRL	END_MRL	VALUE

1	1		5	500
2	5.1		10	600
3	10.1		15	800
4	15.1		20	1000
5	20.1		25	2000
6	25.1		30	3000

In Table-2 (UTL_FINAL) we have Unit_mrl, Ret_val Column Names with Following Data

UNIT_MRL	RET_VAL

7	
11	
16	
26	
3	

Output Result in Table-2 (utl_final) according to following condition

If utl_final.unit_mrl between utl_charges.start_mrl and utl_charges.end_mrl then
utl_final.Ret_val = Utl_charges.value (In case of 7 utl_final.Ret_val = 600 because 7 between 5.1 and 10 in Table-1 (UTL_CHARGES) and so on.


Output will be as follows in Utl_Final table


UNIT_MRL	RET_VAL

7		600
11		800
16		1000
26		3000
3		500


TABLE-1 

INSERT INTO UTL_CHARGES ( LINE_ID, START_MRL, END_MRL, VALUE ) VALUES ( 
1, 1, 5, 500); 
INSERT INTO UTL_CHARGES ( LINE_ID, START_MRL, END_MRL, VALUE ) VALUES ( 
2, 5.1, 10, 600); 
INSERT INTO UTL_CHARGES ( LINE_ID, START_MRL, END_MRL, VALUE ) VALUES ( 
3, 10.1, 15, 800); 
INSERT INTO UTL_CHARGES ( LINE_ID, START_MRL, END_MRL, VALUE ) VALUES ( 
4, 15.1, 20, 1000); 
INSERT INTO UTL_CHARGES ( LINE_ID, START_MRL, END_MRL, VALUE ) VALUES ( 
5, 20.1, 25, 2000); 
INSERT INTO UTL_CHARGES ( LINE_ID, START_MRL, END_MRL, VALUE ) VALUES ( 
6, 25.1, 30, 3000); 
COMMIT;



TABLE-2 (OUTPUT)




INSERT INTO UTL_FINAL ( UNIT_MRL, RET_VAL ) VALUES ( 
7, NULL); 
INSERT INTO UTL_FINAL ( UNIT_MRL, RET_VAL ) VALUES ( 
11, NULL); 
INSERT INTO UTL_FINAL ( UNIT_MRL, RET_VAL ) VALUES ( 
16, NULL); 
INSERT INTO UTL_FINAL ( UNIT_MRL, RET_VAL ) VALUES ( 
26, NULL); 
INSERT INTO UTL_FINAL ( UNIT_MRL, RET_VAL ) VALUES ( 
3, NULL);  
COMMIT;


Thanx

best regards

[Updated on: Tue, 01 April 2014 12:43]

Report message to a moderator

Re: Get value between two fields [message #611345 is a reply to message #611336] Tue, 01 April 2014 13:32 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
I would try an UPDATE with a subquery, structured like this:

update utl_final set ret_val = (select ... from ... where ... > ... and ... <= ... );

have a go at filling the blanks/
Re: Get value between two fields [message #611346 is a reply to message #611336] Tue, 01 April 2014 13:32 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

CREATE TABLE statements are missing.
You can do something like:
SCOTT> desc emp
 Name                             Null?    Type
 -------------------------------- -------- ----------------------
 EMPNO                            NOT NULL NUMBER(4)
 ENAME                                     VARCHAR2(10 CHAR)
 JOB                                       VARCHAR2(9 CHAR)
 MGR                                       NUMBER(4)
 HIREDATE                                  DATE
 SAL                                       NUMBER(7,2)
 COMM                                      NUMBER(7,2)
 DEPTNO                                    NUMBER(2)

SCOTT> desc salgrade
 Name                             Null?    Type
 -------------------------------- -------- ----------------------
 GRADE                            NOT NULL NUMBER
 LOSAL                                     NUMBER
 HISAL                                     NUMBER

SCOTT> select e.ename, e.sal, s.grade
  2  from emp e, salgrade s
  3  where e.sal between s.losal and s.hisal
  4  order by e.ename
  5  /
ENAME             SAL      GRADE
---------- ---------- ----------
ADAMS            1100          1
ALLEN            1600          3
BLAKE            2850          4
CLARK            2450          4
FORD             3000          4
JAMES             950          1
JONES            2975          4
KING             5000          5
MARTIN           1250          2
MILLER           1300          2
SCOTT            3000          4
SMITH             800          1
TURNER           1500          3
WARD             1250          2

Re: Get value between two fields [message #611387 is a reply to message #611346] Wed, 02 April 2014 02:29 Go to previous messageGo to next message
hissam78
Messages: 193
Registered: August 2011
Location: PAKISTAN
Senior Member
Sorry Dear Experts Mr.Michel Cadot and Mr.John Watson
i forgot to upload the table script. but with your given logic, problem resolved. thanks highly appreciated.

Tables Scripts

CREATE TABLE UTL_CHARGES
(
LINE_ID NUMBER,
START_MRL NUMBER,
END_MRL NUMBER,
VALUE NUMBER
)

CREATE TABLE UTL_FINAL
(
UNIT_MRL NUMBER,
RET_VAL NUMBER
)

Following is the Update Query i have used.


UPDATE UTL_FINAL b
SET Ret_val =
(SELECT DISTINCT a.Value
FROM UTL_CHARGES a
WHERE b.Unit_mrl BETWEEN a.start_mrl AND a.end_mrl
)
commit;


best regards
Re: Get value between two fields [message #611392 is a reply to message #611387] Wed, 02 April 2014 02:36 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

The DISTINCT in subquery ould be useless as the subquery should return only one row unless you have overlapped intervals in your UTL_CHARGES table which should then create many problems in your application, I think.

Re: Get value between two fields [message #611393 is a reply to message #611392] Wed, 02 April 2014 02:44 Go to previous message
hissam78
Messages: 193
Registered: August 2011
Location: PAKISTAN
Senior Member
Thanx again for this expert opinion, you are quite right no need of distinct in subquery.

best regards,
Previous Topic: ROW_NUMBER() OVER( ORDER BY 2, 1 )
Next Topic: How to get when_no_data found exception messages using cursor for loop
Goto Forum:
  


Current Time: Fri Apr 26 17:04:42 CDT 2024