Question

Assign employees to table in PL/SQL

Asked by: ksummers

I need help in PL/SQL assigning employees to records based on client and state.

I have a table called EMPLOYEE_STATES with the employee number and the states and client assigned to that employee.

I have a table called WORK_RECORDS with addresses and loan information and employee field.   These records are currently unassigned to any employees.

I need to assign employees to  WORK_RECORDS based on the state and employee in EMPLOYEE_STATE.
So, If two employees are working the same state, I need to divide up the work.  

Attached is the EMPLOYEE_STATES, WORK_RECORDS and WORK_RECORDS assigned.

Please help me write a stored proc to do this.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-12-02 at 08:24:13ID23950191
Tags

PL/SQL

Topics

PL / SQL

,

Oracle 10.x

,

Oracle Database

Participating Experts
2
Points
500
Comments
14

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. PL/SQL
    Hello all, I am trying to write a simple little pl/sql block and keep having problems. I am trying to count the number of rows in a series of tables where a certain value exists. First of all I have a table mcg_tables to hold the list of tables in question and I then popul...
  2. pl/sql arrays
    I wish to work with arrays in pl/sql language, also using loops and 'for' conditions? maybe somebody know how arrays are written in pl/sql?
  3. Receive the list of files in PL\SQL procedure?
    How I can receive the list of files in PL\SQL proc?
  4. PL/sql
    When ever i try to create this procedure it shows the compilation error. create or replace PROCEDURE sp_IDIssue (DF IN Date, DT IN Date) AS BEGIN SELECT Production.Report_Date, Production.Current_Shift, Production.Shift_No, ...
  5. PL/SQL
    Has any of you have experience using PL/SQL Tables ? The Index-by and Nested ones, not Oracle Database Tables which use Cursors. The PL/SQL Tables are similar to the arrays found in other languages. I just need to pass a PL/SQL Collection as a parameter use it in another p...
  6. Returning message to proC code from PL/SQL
    I am calling PL/SQL procedure from proC. I would like to send specific message back to my calling proC code after the procedure has completed. For example, output of DBMS_OUTPUT or exceptions defined by me within the procedure. Not SQLCODE and SQLERRM. How do I do it? Please ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: sdstuberPosted on 2008-12-02 at 21:56:10ID: 23083544

you don't need pl/sql,  you can do it purely with sql.

This won't necessarily create the same sorting in the distribution, but it will create an even distribution.
If sorting is important, it can be modified with the row_number "order by" clauses

SELECT   w.address1, w.state_abbreviation, e.employee_key
  FROM   (SELECT   state_abbreviation,
                   employee_key,
                   ROW_NUMBER() OVER (PARTITION BY state_abbreviation ORDER BY employee_key) rn,
                   COUNT( * ) OVER (PARTITION BY state_abbreviation) cnt
            FROM   employee_states) e,
         (SELECT   address1,
                   state_abbreviation,
                   ROW_NUMBER() OVER (PARTITION BY state_abbreviation ORDER BY address1) rn
            FROM   work_records) w
 WHERE   e.state_abbreviation = w.state_abbreviation AND MOD(w.rn, e.cnt) = e.rn - 1

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen in new window

 

by: nav_kum_vPosted on 2008-12-02 at 22:58:23ID: 23083746

Hi ksummers,

I can understand your question but before i propose some solution/advice, i need to understand as to how did you arrive at the below.

in work_records table,  you have 7 records for state GA and you have two employees working in that state namely 5919 and 2335 but
1) how did you decide that first 4 records should go to 5919 and the remaining 3 records should go to 2335 ? any rationale behind this ?

OR

2)  it is just that you have 7 recoreds and 2 emps, you gave some 4 to one emp say 5919 and the remaining 3 to emp 2335

So can you answer whether you pick 1 ) or 2) from this.

 

by: ksummersPosted on 2008-12-03 at 06:31:54ID: 23086127

sdstuber - i will try your select and let you know how it works.  i believe that will work as long as there is some sort of even distribution.

nav_kum_v - thank you for your questions.  that is what i am trying to arrive at, achieving some sort of even distribution in my sql.  if two employees are assigned the same state, then i need to divide the assignments up some sort of way.  it does not need to be a perfect distribution.  

 

by: sdstuberPosted on 2008-12-03 at 06:43:22ID: 23086261

yes it does an even distribution, or as even as it can.  If you have 2 employees and 5 locations, one of them will get more work.

 

by: ksummersPosted on 2008-12-03 at 09:02:19ID: 23087874

sdstuber - when i run your sql, i am getting more records than expected.  there are additional fields that i did not include in myproblem.  where i should i include them in your sql?  Here is my SQL, obviously a bit longer.

SELECT w.state_abbreviation, e.employee_key
  FROM (SELECT state_abbreviation,
               employee_key,
               ROW_NUMBER() OVER(PARTITION BY state_abbreviation ORDER BY employee_key) rn,
               COUNT(*) OVER(PARTITION BY state_abbreviation) cnt
          FROM ccsd_assignment) e,
       (SELECT /*+index (dp XIE4DELQ_PARCEL)*/
        distinct sl.state_abbreviation,
                 lpp.payee_code,
                 jl.zc_jurisdiction_ldesc,
                 a.ParcelCount,
                 ROW_NUMBER() OVER(PARTITION BY sl.state_abbreviation ORDER BY sl.state_abbreviation) rn
        -- NVL (a.ParcelCount - NVL (b.assignedloans, 0), 0) "Unassigned Count"
          FROM state_list sl,
               lender_payee_profile lpp,
               zc_jur_list jl,
               lender len,
               (SELECT lpp2.payee_code,
                       COUNT(DISTINCT dp.delq_parcel_key) ParcelCount
                  FROM delq_parcel          dp,
                       lender_payee_profile lpp2,
                       loan                 l,
                       lender               len2,
                       lss_tax_line         lss
                 WHERE nvl(l.inactive_ind, 'NO') = 'NO'
                   AND NVL(len2.ccsd_research_ind, 'NO') = 'YES'
                   AND dp.lender_payee_code = lpp2.lender_payee_code
                   and lpp2.LENDER_NUMBER = 693
                   and nvl(lpp2.inactive_ind, 'NO') = 'NO'
                   and l.LENDER_NUMBER = 693
                   and nvl(len2.inactive_ind, 'NO') = 'NO'
                   AND dp.delq_loan_key = l.delq_loan_key
                   AND l.LENDER_NUMBER = 693
                   AND l.loan_number = lss.loan_number
                   AND dp.lender_payee_code = lss.lender_payee_code
                   AND dp.payee_parcel_number = lss.payee_parcel_number
                   AND dp.delq_letter_status_mldesc =
                       'Cycle Complete - Still Delinquent'
                   AND nvl(dp.adverse_action_date, sysdate + 91) >
                       sysdate + 90
                   AND nvl(dp.DELQ_VERIFIED_IND, 'NO') = 'YES'
                   AND nvl(dp.TAX_ALL_PAID_IND, 'NO') = 'NO'
                   and nvl(dp.NEXT_ACTION_TYPE_MLDESC, 'zzz') <>
                       'No Action Taken'
                   and nvl(dp.NEXT_ACTION_TYPE_MLDESC, 'zzz') <>
                       'Paid - Without forced escrow'
                   and nvl(dp.NEXT_ACTION_TYPE_MLDESC, 'zzz') <>
                       'Paid - With Forced Escrow'
                   and nvl(dp.NEXT_ACTION_TYPE_MLDESC, 'zzz') <>
                       'Paid - Without Forced Escrow'
                   and dp.CCSD_COMPLETED_DATE is null
                   AND dp.CCSD_ASSIGNED_EMPLOYEE_KEY is null
                 GROUP BY lpp2.payee_code) a
         WHERE a.payee_code = lpp.payee_code
           and NVL(len.ccsd_research_ind, 'NO') = 'YES'
           AND lpp.LENDER_NUMBER = 693
           and nvl(len.inactive_ind, 'NO') = 'NO'
           AND lpp.payee_code = jl.zc_jurisdiction_code
           and sl.FIPS_STATE_CODE = jl.FIPS_STATE_CODE) w
 WHERE e.state_abbreviation = w.state_abbreviation
   AND MOD(w.rn, e.cnt) = e.rn - 1

 

by: sdstuberPosted on 2008-12-03 at 09:33:23ID: 23088170

depends on what output you are expecting.  please post "create table" ddl for your objects with sample data and expected output.  It will be MUCH more helpful if you post it as text rather than images, that way we can use what you post directly rather than retyping it all in ourselves.  Not only is it more efficient but less error prone.

 

by: ksummersPosted on 2008-12-03 at 10:24:50ID: 23088615

yes, i agree.  i guess my question is maybe this is happening because i am not including fields in the row_num rn field.  the second set of sql "w" SHOULD return 221 records for me, but it is returning 1422 records when the row_num is included.

 

by: sdstuberPosted on 2008-12-03 at 10:36:35ID: 23088715

probably you want to do the numbering after you do distinct...

if so, change your "w" view to to be this...

this is still somewhat of a guess, if you provide the ddl and sample data, I can build the test case and confirm

(SELECT state_abbreviation,
        payee_code,
        zc_jurisdiction_ldesc,
        parcelcount,
        ROW_NUMBER() OVER (PARTITION BY sl.state_abbreviation ORDER BY sl.state_abbreviation) rn
   FROM (SELECT                                                      /*+index (dp XIE4DELQ_PARCEL)*/
               DISTINCT
                sl.state_abbreviation, lpp.payee_code, jl.zc_jurisdiction_ldesc, a.parcelcount
           -- NVL (a.ParcelCount - NVL (b.assignedloans, 0), 0) "Unassigned Count"
           FROM state_list sl,
                lender_payee_profile lpp,
                zc_jur_list jl,
                lender len,
                (  SELECT lpp2.payee_code, COUNT(DISTINCT dp.delq_parcel_key) parcelcount
                     FROM delq_parcel dp,
                          lender_payee_profile lpp2,
                          loan l,
                          lender len2,
                          lss_tax_line lss
                    WHERE NVL(l.inactive_ind, 'NO') = 'NO'
                      AND NVL(len2.ccsd_research_ind, 'NO') = 'YES'
                      AND dp.lender_payee_code = lpp2.lender_payee_code
                      AND lpp2.lender_number = 693
                      AND NVL(lpp2.inactive_ind, 'NO') = 'NO'
                      AND l.lender_number = 693
                      AND NVL(len2.inactive_ind, 'NO') = 'NO'
                      AND dp.delq_loan_key = l.delq_loan_key
                      AND l.lender_number = 693
                      AND l.loan_number = lss.loan_number
                      AND dp.lender_payee_code = lss.lender_payee_code
                      AND dp.payee_parcel_number = lss.payee_parcel_number
                      AND dp.delq_letter_status_mldesc = 'Cycle Complete - Still Delinquent'
                      AND NVL(dp.adverse_action_date, SYSDATE + 91) > SYSDATE + 90
                      AND NVL(dp.delq_verified_ind, 'NO') = 'YES'
                      AND NVL(dp.tax_all_paid_ind, 'NO') = 'NO'
                      AND NVL(dp.next_action_type_mldesc, 'zzz') <> 'No Action Taken'
                      AND NVL(dp.next_action_type_mldesc, 'zzz') <> 'Paid - Without forced escrow'
                      AND NVL(dp.next_action_type_mldesc, 'zzz') <> 'Paid - With Forced Escrow'
                      AND NVL(dp.next_action_type_mldesc, 'zzz') <> 'Paid - Without Forced Escrow'
                      AND dp.ccsd_completed_date IS NULL
                      AND dp.ccsd_assigned_employee_key IS NULL
                 GROUP BY lpp2.payee_code) a
          WHERE a.payee_code = lpp.payee_code
            AND NVL(len.ccsd_research_ind, 'NO') = 'YES'
            AND lpp.lender_number = 693
            AND NVL(len.inactive_ind, 'NO') = 'NO'
            AND lpp.payee_code = jl.zc_jurisdiction_code
            AND sl.fips_state_code = jl.fips_state_code))

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:

Select allOpen in new window

 

by: ksummersPosted on 2008-12-03 at 11:25:40ID: 23089146

what do you mean ddl?  i will continue playing with this sql.  i am getting close!

 

by: sdstuberPosted on 2008-12-03 at 11:37:02ID: 23089251

ddl is data definition language.

the "sql" commands to create the objects you are working with.

for example....

create table mytable (
 col1 number,
 col2 date,
 col3 varchar2(20)
)

 

by: sdstuberPosted on 2008-12-11 at 10:55:49ID: 23151105

do you need further assistance?

 

by: ksummersPosted on 2008-12-11 at 10:59:49ID: 23151134

yes, i got sidetracked.  i will be working on this today and tomorrow.  i will make some comments here soon.

 

by: ksummersPosted on 2008-12-11 at 13:02:22ID: 23152246

sdstuber - i looked back over your SQL and did some tests here.  i had the counts wrong last time we corresponded.  it seems to work perfect, thank you!  i will award you the points.  if i have anymore questions, i will contact you here.  thanks again!

 

by: sdstuberPosted on 2008-12-11 at 22:21:54ID: 23155313

glad I could help!

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...