Home » SQL & PL/SQL » SQL & PL/SQL » Compare values based on range
Compare values based on range [message #643970] Sat, 24 October 2015 07:12 Go to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
Hi,

I am having below tables

T1
     
ID	Balance
1	500
2	50
3	1000
 
T2
 
     
Min	Max	Int
0	100	2
101	500	1
501	1000 3

Output required
 
     
ID	R1	R2	R3	R1_int	R2_int	R3_int
1	0	500	0	0	1	0
2	50	0	0	2	0	0
3	0	0	1000	0	0	3
 


i need to check balance falling in which range and take the respective R1_int

for Eg. 500 falls between second range (R2)i.e 101-500 and take the int value which is 1 and place the value as R2_int(second range int value) with rest of R1_int and R3_int as zero

ID R1 R2 R3 R1_int R2_int R3_int
1 0 500 0 0 1 0


create table T1(id number,balance number);
create table T2(min number,max number,int number);
 
insert into T1
select 1,500 from dual
union all
select 2,50 from dual
union all
select 3,1000 from dual;
 
insert into T2
select 0,100,2 from dual
union all
select 101,500,1 from dual
union all
select 501,1000,3 from dual;
Re: Compare values based on range [message #643971 is a reply to message #643970] Sat, 24 October 2015 07:40 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

And what is your problem writing this query?
What did you try so far?
It seems to me that a simple join and some decode or case do the trick.

Re: Compare values based on range [message #643973 is a reply to message #643970] Sat, 24 October 2015 07:55 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
I have tried but not able to achieve


select a.id,case when a.balance between b.min and b.max then a.balance else 0 end from T1 a, T2 b 
group by a.id,
a.balance,
b.min,
b.max;

Re: Compare values based on range [message #643974 is a reply to message #643973] Sat, 24 October 2015 07:59 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Of course, you want 7 columns and your select returns only 4.
Try again.
And please format your statement.

Hints:
1/ this is a pivot query
2/ why did you need to use a "group by"?

Re: Compare values based on range [message #643975 is a reply to message #643974] Sat, 24 October 2015 08:32 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
I don't want to use Pivot rather than using case /decode statement
Re: Compare values based on range [message #643976 is a reply to message #643975] Sat, 24 October 2015 08:34 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Use what you want; "pivot" is here a method NOT a syntax.

[Updated on: Sat, 24 October 2015 08:37]

Report message to a moderator

Re: Compare values based on range [message #643977 is a reply to message #643975] Sat, 24 October 2015 08:35 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
rohit_shinez wrote on Sat, 24 October 2015 06:32
I don't want to use Pivot rather than using case /decode statement


You should use that which produces the desired results since nobody will ever see underlying SQL.
Re: Compare values based on range [message #643978 is a reply to message #643977] Sat, 24 October 2015 08:51 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
I tried something like this but struck


SELECT    t1.id
,         CASE WHEN t2.int = 2 THEN t1.balance ELSE 0 END   AS r1
,         CASE WHEN t2.int = 1 THEN t1.balance ELSE 0 END   AS r2
,         CASE WHEN t2.int = 3 THEN t1.balance ELSE 0 END   AS r3
FROM      t1
JOIN      t2   ON   t1.balance  BETWEEN  t2.min
                                AND      t2.max
ORDER BY  t1.id

Re: Compare values based on range [message #643979 is a reply to message #643978] Sat, 24 October 2015 08:58 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

1/ As you want to compare all rows from T1 to all rows from T2 you have to use a cross join.
2/ As you want the result in columns and not rows the you have to use a "pivot" method ("MAX(DECODE","MAX(CASE" or "PIVOT" as you want.

Try to first do 1 then, when you have the expected result (rows) for 1, do 2.

Re: Compare values based on range [message #643980 is a reply to message #643970] Sat, 24 October 2015 09:14 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
How can I use second method with decode and max I am unable to do
Re: Compare values based on range [message #643981 is a reply to message #643980] Sat, 24 October 2015 09:18 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

First do point 1.

Re: Compare values based on range [message #643982 is a reply to message #643981] Sat, 24 October 2015 12:08 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
SELECT t1.id
, CASE WHEN t2.int = 2 THEN t1.balance ELSE 0 END AS r1
, CASE WHEN t2.int = 1 THEN t1.balance ELSE 0 END AS r2
, CASE WHEN t2.int = 3 THEN t1.balance ELSE 0 END AS r3
FROM t1,t2
where t1.balance BETWEEN t2.min
AND t2.max;
Re: Compare values based on range [message #643983 is a reply to message #643982] Sat, 24 October 2015 12:19 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Is this not your previous query we know is wrong?

What is the reason of these "CASE WHEN t2.int = "?

Re: Compare values based on range [message #643984 is a reply to message #643983] Sat, 24 October 2015 12:39 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
I have tried that query to get the output, but i am not getting
Re: Compare values based on range [message #643985 is a reply to message #643984] Sat, 24 October 2015 13:09 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

As I said

Quote:
1/ As you want to compare all rows from T1 to all rows from T2 you have to use a cross join.

A cross join is also known as Cartesian join.

Re: Compare values based on range [message #643986 is a reply to message #643985] Sat, 24 October 2015 13:20 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
yeah but i am not getting the desired output
Re: Compare values based on range [message #643987 is a reply to message #643986] Sat, 24 October 2015 13:22 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Of course as you are not doing a cross join.

Re: Compare values based on range [message #643988 is a reply to message #643987] Sat, 24 October 2015 13:24 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
SELECT    t1.id
,         CASE WHEN t2.int = 2 THEN t1.balance ELSE 0 END   AS r1
,         CASE WHEN t2.int = 1 THEN t1.balance ELSE 0 END   AS r2
,         CASE WHEN t2.int = 3 THEN t1.balance ELSE 0 END   AS r3
FROM      t1
cross join t2;

ID	R1	R2	R3
1	500	0	0
2	50	0	0
3	1000	0	0
1	500	0	0
2	50	0	0
3	1000	0	0
1	0	500	0
2	0	50	0
3	0	1000	0
1	0	500	0
2	0	50	0
3	0	1000	0
1	0	0	500
2	0	0	50
3	0	0	1000
1	0	0	500
2	0	0	50
3	0	0	1000


Re: Compare values based on range [message #643990 is a reply to message #643988] Sat, 24 October 2015 13:30 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Michel Cadot wrote on Sat, 24 October 2015 19:19
...
What is the reason of these "CASE WHEN t2.int = "?

Re: Compare values based on range [message #643991 is a reply to message #643990] Sat, 24 October 2015 13:34 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
there is no reason i have written the query in such a way to get the output but no specific reason
Re: Compare values based on range [message #643992 is a reply to message #643991] Sat, 24 October 2015 13:39 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

You randomly write your query? I understand why you can't get the result. There is very few chances you get the good result with this method.

Remove that and just use a "select *" to see what happens.
Then think how to modify the select expressions to get the good values (note not the final output just the good value, we are still in step 1).
So what output you get with "select *" and what should be the rows?.

Re: Compare values based on range [message #643993 is a reply to message #643992] Sat, 24 October 2015 13:50 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
ok i am in first step now

SELECT   *
FROM      t1
cross join t2;

ID	BALANCE	MIN	MAX	INT
1	500	0	100	2
2	50	0	100	2
3	1000	0	100	2
1	500	0	100	2
2	50	0	100	2
3	1000	0	100	2
1	500	101	500	1
2	50	101	500	1
3	1000	101	500	1
1	500	101	500	1
2	50	101	500	1
3	1000	101	500	1
1	500	501	1000	3
2	50	501	1000	3
3	1000	501	1000	3
1	500	501	1000	3
2	50	501	1000	3
3	1000	501	1000	3

Re: Compare values based on range [message #643994 is a reply to message #643993] Sat, 24 October 2015 13:55 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

OK, now for each row, what are the values you want?
(Note it'd be easier if you order this output by id and min.)

Re: Compare values based on range [message #643995 is a reply to message #643994] Sat, 24 October 2015 14:05 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
ID	BALANCE	MIN	MAX	INT
1	500	0	100	2
1	500	0	100	2
1	500	101	500	1
1	500	101	500	1
1	500	501	1000	3
1	500	501	1000	3
2	50	0	100	2
2	50	0	100	2
2	50	101	500	1
2	50	101	500	1
2	50	501	1000	3
2	50	501	1000	3
3	1000	0	100	2
3	1000	0	100	2
3	1000	101	500	1
3	1000	101	500	1
3	1000	501	1000	3
3	1000	501	1000	3

each row i wanted something like this

expalined the first row

ID	BALANCE	MIN	MAX	INT	R1	R2	R3	R4	R1_INT	R2_INT	R3_INT	R4_INT
1	500	0	100	2	0	500	0	0	0	2	0	0
1	500	0	100	2								
1	500	101	500	1								
1	500	101	500	1								
1	500	501	1000	3								
1	500	501	1000	3								
2	50	0	100	2								
2	50	0	100	2								
2	50	101	500	1								
2	50	101	500	1								
2	50	501	1000	3								
2	50	501	1000	3								
3	1000	0	100	2								
3	1000	0	100	2								
3	1000	101	500	1								
3	1000	101	500	1								
3	1000	501	1000	3								
3	1000	501	1000	3								

Re: Compare values based on range [message #643996 is a reply to message #643995] Sat, 24 October 2015 14:15 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
(note not the final output just the good value, we are still in step 1).


Please CAREFULLY read and try to understand ALL what I post.

Re: Compare values based on range [message #643997 is a reply to message #643996] Sat, 24 October 2015 14:38 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
Sorry i am not getting
Re: Compare values based on range [message #644005 is a reply to message #643997] Sun, 25 October 2015 07:13 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
can you advise
Re: Compare values based on range [message #644009 is a reply to message #644005] Sun, 25 October 2015 13:28 Go to previous message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

You will never be able to do it so I give you but this is the last time I try to help you.
You should envisage to either take courses on algorithm, RBDMS and SQL or change your job to no more encounter SQL.

SQL> select t1.id,
  2         max(case when t2.id = 1 and t1.balance between t2.min and t2.max then t1.balance else 0 end) r1,
  3         max(case when t2.id = 2 and t1.balance between t2.min and t2.max then t1.balance else 0 end) r2,
  4         max(case when t2.id = 3 and t1.balance between t2.min and t2.max then t1.balance else 0 end) r3,
  5         max(case when t2.id = 1 and t1.balance between t2.min and t2.max then t2.int else 0 end) r1_int,
  6         max(case when t2.id = 2 and t1.balance between t2.min and t2.max then t2.int else 0 end) r2_int,
  7         max(case when t2.id = 3 and t1.balance between t2.min and t2.max then t2.int else 0 end) r3_int
  8  from t1, ( select t2.*, row_number() over (order by min) id from t2 ) t2
  9  group by t1.id
 10  order by t1.id
 11  /
        ID         R1         R2         R3     R1_INT     R2_INT     R3_INT
---------- ---------- ---------- ---------- ---------- ---------- ----------
         1          0        500          0          0          1          0
         2         50          0          0          2          0          0
         3          0          0       1000          0          0          3

Previous Topic: Is there a limitation of connect by for Partitioned tables
Next Topic: To Split data based on dates
Goto Forum:
  


Current Time: Sun Jul 05 07:07:19 CDT 2026