Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 replace a cursor using join? challenging Question

Author  Topic 

mrajakrishnan
Starting Member

3 Posts

Posted - 2009-10-28 : 00:14:42
There is a table as follows
.
Work |Days required |Employee who all can do this work.
------------------------------------------------------------------------
work1 |2 |Employee1 and employee 2
work2 |3 |Employee3 and employee4
work3 |2 |Employee1 and employee 2
work4 |1 |Employee3 and employee4
work5 |2 |Employee1 and employee 2
work6 |2 |Employee3 and employee4
work7 |2 |Employee1 and employee 2
work8 |2 |Employee3 and employee4
work9 |2 |Employee1 and employee 2
work10 |2 |Employee3 and employee4
work11 |2 |Employee1 and employee 2
work12 |2 |Employee3 and employee4
work13 |2 |Employee1 and employee 2
work14 |2 |Employee3 and employee4
work15 |2 |Employee1 and employee 2
work16 |2 |Employee3 and employee4
work17 |2 |Employee1 and employee 2
work18 |2 |Employee3 and employee4
work19 |2 |Employee1 and employee 2
work20 |2 |Employee3 and employee4

This table will tell how many days required to do a work and who all can do this work.

Now the result I need through a join is

For example today is 28-oct

Output |Output |Output |Reason(Just to explain you)
-----------------------------------------------------------------------------------------
work1 |28,29 Oct |Employee1 |Emp1 or Emp2 can do as both are free I can assign any one
work2 |28,29,30 Oct |Employee3 |Emp3 and Emp4 are free so I can assign work any one
work3 |28,29 Oct |Employee2 |emp1 is busy on work1 and emp2 is free so assigning work to emp2
work4 |28-Oct |employee4 |emp3 is busy on work2 and emp4 is free so assigning work to emp4
work5 |30,31 |employee1 |emp1 and emp2 will be free on 30th onwards,so any one I can put for this work
work6 |29,30 Oct |employee4 |emp3 will get free on 31st but emp4 will be free on 29th, so will provide this work to emp4 so that he can start work from 29th
work7 |30,31 Oct |employee2 |
work8 |31oct,1st nov |employee3 |
work9 |30,31oct |employee2 |
work10 | | |
............ | | |
............ | | |
............ | | |
............ | | |
............ | | |
............ | | |
........... | | |
| | |
| | |


Similarly all the work should get assigned.

The ouput of join should give me the date and Employee details.

Date is based on starting from today date, then how many required to do this work and the availability of the Employee



Currently I am doing the same using cursor, but as work and employee list is high it is taking hell lot of time, can any one suggest me how to get this same result using joins instead of cursor.


Thanks



khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-10-28 : 09:51:53
Question
1. in your table, the column "Employee who all can do this work" will contain multiple value in the same column like "Employee1 and employee 2" ? Is it always 2 Employee in there separated by "AND" ? Will you have more than 2 Employee in that column ?

2. in the reason, how do you determine that an employee is free ?

3. is there any sequence of processing of assigning employee to work ? Is it in the order of work column ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

mrajakrishnan
Starting Member

3 Posts

Posted - 2009-10-31 : 02:39:56
Thanks in advance for the respose

Basically there will be a table saying who all can do that work and how much time required to do the work. The number of person who can do the work can be more than 2 ,it will not be in saame column, it will be in differnt rows

For eg)
Work| (Employee who all can do that work)| Time required to complete this work
Work1| Employee1| 16 hours
Work1| Employee2|16 hours
work1| Employee3|16 hours

Work2| Employee2|16 hours
work2| Employee5|16 hours
work2| Employee6| 16 hours


etc

The above says work1 can be done eithere Employee2 or Employee2 or Employee3 depends who is free. One work will be always by one person, no sharing work.


Employee table says working hours of each employee per day, For example Employee 1 working hours might be 8 hours per day,Employee2 standard working time might be 16hours per days. It says Work1 can be completed by Employee1 in 2 days if Employee2 do the same work he will complete in one day.


So If employee1 take this job next employee1 will be free is after 2 days
But If employee2 take this job next employee2 will be free is after 1 days, so that he can take next work on 2nd day/





Employ

Go to Top of Page
   

- Advertisement -