Home » SQL & PL/SQL » SQL & PL/SQL » Help in designing a query in PL/SQL?
Help in designing a query in PL/SQL? [message #517107] |
Fri, 22 July 2011 10:57 |
|
akpaga
Messages: 52 Registered: July 2011
|
Member |
|
|
Hi all
i have query which scores a customer details on some factors and gives it a rank
I have a table called agents which holds the agents details.
This is how i get the rank:
select *
from (
select customer_name ,agent_number
customer_number,
customer_score,
agent_score,
customer_score+agent_score total_score,
Rank() over(PARTITION BY Name ORDER BY (customer_score+agent_score) DESC) as Ranker
from customer,agents )
where ranker = 1
The agents table has agent_number and agent_Name....
By the above query i check various score against all available agents and assign customers to agents who have highest score or rank is 1
But my problem is who to loop through the process.. i mean after the intial customers are assinged with agents
it should run again for remaining customers and for agents who are free(and rank for them is 1) should be checked for assignment
Once all agents are assigned but still more customers are available then agents even thoug they are already assigned they can be assigned now since no agent is free...
Hope my quetsion is clear..sorry for my english..newbie to PL/SQl
|
|
|
|
Re: Help in designing a query in PL/SQL? [message #517110 is a reply to message #517108] |
Fri, 22 July 2011 11:22 |
|
akpaga
Messages: 52 Registered: July 2011
|
Member |
|
|
THANKS FOR YOU REPLY Barabara...
But can you give some code example for the followinging test cases
Customer Table
Customer_name,Customer_num, customer_score
John,12,35,
James,13,55
harvard,14,25
Jake,15,60
Jill,16,35,
Oria,15,100
Agent table
Agent_number,agent_name,Agent_Score
1,Dennis,34,
2,Kelly,65
3Jackson,45
Can you please provide some code...
|
|
|
|
Re: Help in designing a query in PL/SQL? [message #517112 is a reply to message #517111] |
Fri, 22 July 2011 11:34 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
And for starters you query is not correct. If more that one customer/agent have top score either two customers will be paired with same agent or two agents will be paired with same customer. Change RANK to ROW_NUMBER. Also, where are you planning to store customer/agent pairs? is there a third table?
SY.
|
|
|
|
Re: Help in designing a query in PL/SQL? [message #517118 is a reply to message #517113] |
Fri, 22 July 2011 13:22 |
|
akpaga
Messages: 52 Registered: July 2011
|
Member |
|
|
Sorry i am new to the forum as well... I have written Create and insert statements
CREATE TABLE Customer(
Customer_name varchar2,Customer_num number, customer_score number
) ;
CREATE TABLE Agent(
Agent_number number,agent_name varchar2,Agent_Score number
);
INSERT INTO Customer VALUES ('John',12,35);
INSERT INTO Customer VALUES ('James',13,55);
INSERT INTO Customer VALUES ('harvard',14,25);
INSERT INTO Customer VALUES ('Jake',15,60);
INSERT INTO Customer VALUES ('Jill',16,35,);
INSERT INTO Customer VALUES ('Oria',15,100);
INSERT INTO Agent VALUES (1,'Dennis',34,);
INSERT INTO Agent VALUES (2,'Kelly',65);
INSERT INTO Agent VALUES (3,'Jackson',45);
So finally what i wnat is
I will add the agent_score+Customer care and check which Customer has highest score for a Tech and then assign that customer with the agent provided The agents are free... So after the three agents have a customer... then i have to go through again and assign remaining Customer with the same three agents follwing similar process...Hope I am clear thsi time....
Thank you for your time ...
|
|
|
|
|
Re: Help in designing a query in PL/SQL? [message #517128 is a reply to message #517118] |
Fri, 22 July 2011 14:33 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
akpaga wrote on Fri, 22 July 2011 14:22
I will add the agent_score+Customer care and check which Customer has highest score for a Tech and then assign that customer with the agent provided The agents are free... So after the three agents have a customer... then i have to go through again and assign remaining Customer with the same three agents follwing similar process...
And why do you need to add cuatomer scote to agent score if you are assigning customer to each agent? If I correctly understood it you are trying to assign agents to customers in round-robin manner based on customer and agent scores. Customer with highest score gets agent with highest score. Customer with second highest score gets agent with second highest score. And so on until we assign customers to all agents. then we repeat process with remaining customers. If so, all you need is:
with c1 as (
select c.*,
row_number() over(order by customer_score desc) customer_rank
from customer c
),
a1 as (
select a.*,
row_number() over(order by agent_score desc) agent_rank,
count(*) over() agent_count
from agent a
)
select customer_name,
customer_num,
customer_score,
agent_name,
agent_number,
agent_score,
customer_score + agent_score total_score
from c1,
a1
where trunc((customer_rank - 1)/agent_count) * agent_count + agent_rank = customer_rank
order by customer_rank,
agent_rank
/
CUSTOMER_NAME CUSTOMER_NUM CUSTOMER_SCORE AGENT_NAME AGENT_NUMBER AGENT_SCORE TOTAL_SCORE
------------- ------------ -------------- ---------- ------------ ----------- -----------
Oria 15 100 Kelly 2 65 165
Jake 11 60 Jackson 3 45 105
James 13 55 Kelly 2 65 120
John 12 35 Jackson 3 45 80
Jill 16 35 Kelly 2 65 100
harvard 14 25 Jackson 3 45 70
6 rows selected.
SQL>
SY.
[Updated on: Fri, 22 July 2011 14:46] Report message to a moderator
|
|
|
|
Re: Help in designing a query in PL/SQL? [message #517132 is a reply to message #517131] |
Fri, 22 July 2011 15:03 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Assume customer1 has score 50, customer2 score 40, customer3 score 30, customer4 score 20 and customer5 score 10. Agent1 has score 50, agent2 has score 40 and agent3 has score 30.
What agents according to your rules should be assigned to what customers? And why?
SY.
|
|
|
Re: Help in designing a query in PL/SQL? [message #517135 is a reply to message #517131] |
Fri, 22 July 2011 15:10 |
|
akpaga
Messages: 52 Registered: July 2011
|
Member |
|
|
customer , customerscore, agentscore, totalscore, agent, rank
customer1, 50,50,100,agent1,1
customer2, 40,50,90,agent1,2
customer3, 30,50,80,agent1,3
customer4, 20,50,70,agent1,4
customer1, 50,40,90,agent2,1
customer2, 40,40,80,agent2,2
customer3, 30,40,70,agent2,3
customer4, 20,50,70,agent2,3
customer1, 50,30,80,agent3,1
customer2, 40,30,70,agent3,2
customer3, 30,30,60,agent3,3
customer4, 20,30,50,agent3,3
Since customer 1 is having rank1 with all agents then agent with highest score(tie breaker) whch is agent 1 is assigned to customer 1 then the process starts again
for customer 2,customer3,customer 4 with agent2,agent3,....
This process continues until all customers have agent assigned..
Thank you
[Updated on: Fri, 22 July 2011 15:11] Report message to a moderator
|
|
|
Re: Help in designing a query in PL/SQL? [message #517137 is a reply to message #517135] |
Fri, 22 July 2011 15:25 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
And this is exactly what my code does. It ranks all customers. It ranks all agents. Then it assigns customer ranked 1 to agent ranked 1. Customer ranked 2 to agent ranked 2... Until all agents are assigned a customer. Then is takes next highest customer and assigns it to agent 1, and so on and so forth until all customers have agent assigned.
SY.
|
|
|
Re: Help in designing a query in PL/SQL? [message #517138 is a reply to message #517137] |
Fri, 22 July 2011 18:43 |
|
akpaga
Messages: 52 Registered: July 2011
|
Member |
|
|
Hi syakobson...
i have run into strange problemmm ..my senior developer has thrown light on new stuff..
This is the new scenario
My new agent table is as follwos
agent_num,agent_name,agent_score,customer_num
1,John,35,12
2,Kelly,25,12
3,Jackson,45,12
1,John,55,13
2,Kelly,65,13
3,Jackson,75,13
1,John,95,14
2,Kelly,15,14
3,Jackson,25,14
here agent score is related to customer number..
Now when i add the agent score to customer_score it should be compared only on the customer number
So my customer table :
Customer_name,Customer_num, customer_score
John,12,35,
James,13,55
harvard,14,25
So now you add the customer_score with the agent_score based on customer number and who ever has highest score that agent is assigned to the Customer and so..on
for example:
John,12,35,- Agent3 jackson is assigned since customerscore 35+agent_score 45 =80 -rank 1
James,13,55 - agent 2 Kelly since 55+55=110-rank 1( agent 3 is not considered since he is already assigned with a customer)
harvard,14,25 - agent 1 since he is remaining agent....
Thank you for helping me so far.... I really appreciate it....
|
|
|
Goto Forum:
Current Time: Fri Apr 19 22:57:02 CDT 2024
|