create table label_table(
label_id number not null,
label_name varchar2(50),
no_months number not null,
no_of_mobile_sims number not null,
no_of_services number not null,
delayed number not null,
value_of_client number(6,2),
constraint pk PRIMARY KEY(label_id)
);
create table client(
customer_id number not null,
customer_name varchar2(50) not null,
no_months number not null,
no_of_mobile_sims number not null,
no_of_services number not null,
delayed number not null,
value_of_client number(6,2),
constraint pk_key PRIMARY KEY(customer_id)
) ;
SELECT
l.label_id,
l.label_name,
k.customer_id,
k.customer_name
FROM
label_table l
inner JOIN client k ON k.value_of_client>l.value_of_client
ORDER BY 1;
If we do a select * from client we have this output:
13 x_name 3 4 2 0 74.38
And if we do a select * from label_table we this output:
1 Diamond 2 2 2 0 70
2 Gold 2 1 2 2 50
We have our client x_name who join our phone company let s say and i want to have a report where it will say that x_name is a diamond client based on a comparison between the common columns of these 2 tables and i want an output like this:
13 x_name Diamond
And i want to this with a join but the problem is every time i try my client will be identified with both labels because he respect the conditions(>,<,!=,=) of both labels. The idea is i have 16 clients that have different values and i want to put every one of them in a unique label not in more based on this 2 tables. For example client.no_of_months>label.table.no_of_months,client.value_of_client>label.client.value_of_client then a label but how i said upper i need every client to be in his first choice label if a client is a diamond the output will be diamond not diamond,gold,silver,bronze even if he can be in that labels too.
I guess you may try below query -
SELECT customer_id, customer_name, label_name
FROM (SELECT k.customer_id,
k.customer_name,
l.label_name,
ROW_NUMBER() OVER(PARTITION BY k.customer_id ORDER BY l.value_of_client DESC) RN
FROM label_table l
inner JOIN client k ON k.value_of_client>l.value_of_client)
WHERE RN = 1
ORDER BY 1;
SELECT
k.customer_id,
k.customer_name,
l.label_name
FROM
label_table l
right JOIN client k ON k.value_of_client != l.value_of_client
where l.label_id=(case when k.value_of_client>(select value_of_client from label_table where label_id=1)
and k.delayed=(select delayed from label_table where label_id=1)
and k.no_months >(select no_months from label_table where label_id=1)
and k.no_of_services>=(select no_of_services from label_table where label_id=1)
and k.no_of_mobile_sims>=(select no_of_mobile_sims from label_table where label_id=1)
then 1
when k.value_of_client > (select value_of_client from label_table where label_id=2)
and k.delayed<(select delayed from label_table where label_id=2)
and k.no_months >=(select no_months from label_table where label_id=2)
and k.no_of_services>=(select no_of_services from label_table where label_id=2)
and k.no_of_mobile_sims>(select no_of_mobile_sims from label_table where label_id=2)
then 2
when k.value_of_client>(select value_of_client from label_table where label_id=3)
and k.delayed<(select delayed from label_table where label_id=3)
and k.no_months >(select no_months from label_table where label_id=3)
and k.no_of_services>(select no_of_services from label_table where label_id=3)
and k.no_of_mobile_sims>(select no_of_mobile_sims from label_table where label_id=3)
then 3
else 4
end)
ORDER BY 1;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.