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
 General SQL Server Forums
 New to SQL Server Programming
 suggest a solution for this

Author  Topic 

edumca
Starting Member

3 Posts

Posted - 2011-04-02 : 04:06:23
Hello there,
I have a table in which employee and projects assigned to him are mapped
my problem is to calculate the project weights of the employees.
for example
if emp 1 has 3 projects 10,20,30
for duration 1/4/09 - 30/4/09 , 10/4/09-25/4/09, 15/4/09 - 30/4/09
then weight for project 10 will be (9/30)*100+((5/2)/30)*100+((11/3)/30)*100+((5/2)/30)*100

similarly for projects 20 and 30 so that total project weights shld be 100


-------------------------- proj 10
------------- proj 20
-------------- proj 30

I hv tried a lot but can not find suitable solution
projects assigned can be 1/2/3/........n
Pls help me find the solution


slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2011-04-02 : 04:24:04
what is the formula that you calculate the weight?
Go to Top of Page

mmarovic
Aged Yak Warrior

518 Posts

Posted - 2011-04-02 : 06:04:38
Not sure I got the requirements right. However you can try something like next code. I don't have sql server here installed, so I did not test it. It might be an error somewhere. The precondition is to have pre-populate calendar table.

select m.employeeId, m.projectId, sum(100.0/t.projectsCount)/30
from projectEmployeeMap m
join (
select map.employeeId, [date], cast(count(*) as money) as projectsCount
from projectEmployeeMap map
join calendar cal on map.start <= cal.[date] and map.end >= cal.[date]
group by map.employeeId, map.projectId, [date]
where t.[date] between >= '20090401' and t.[date] < '20090501'
) as t
on t.employeeId = m.employeeId
group by m.employeeId, m.projectId


Mirko

My blog: http://mirko-marovic-eng.blogspot.com/
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-04-03 : 16:38:29
Please post real DDL. Learn to use ISO-11179 rules for the data element names, avoid needless dialect and use ISO-8601 temporal formats, codes and so forth. People cannot read your mind, so post your code and clear specs if you really want help.

--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page

edumca
Starting Member

3 Posts

Posted - 2011-04-05 : 04:59:18
to make my self more clear

my table structure is
empid prjid prjstartDt prjEndDt duration(prjstartDt-prjEndDt)
1 10 01/04/2009 23/04/2009 23
1 20 04/04/2009 05/06/2009
1 30 15/05/2009 30/09/2009


these are the projects assigned to him in the duration 01/04/2009 - 30/09/2009

so total no of days in this cycle are 183
so to calculate the weight for prjid 10
i will first find out the no. of days for which this employee has exclusively worked for this project i.e 3 days
no. of days for which the duration of the project 10 is overlapping with other projects
i.e. 8 days the employee has worked on this project along with 20 and 30, so these days i will divide by 3
from 04/04/09 to 15/04/09 he has worked on 10 and 20 so total no of overlapping days are 20 so we will consider 20/2 days

so weight for project 10 is
(3/183)*100+((8/3)/183)*100+((20/2)/183)*100

similarly v need to calculate the weights for project 20 and 30
I hope i m clear now.

Please suggest a solution
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-04-05 : 05:29:18
When is EmpID 1 ever working in 3 projects simultaneously?
ProjID 10 ends 23rd of April and Proj30 starts 15th of May...

Anyway, here is a fast solution
DECLARE	@Sample TABLE
(
EmpID INT,
ProjID INT,
PrjStartDt DATE,
PrjEndDt DATE,
Duration AS (1 + DATEDIFF(DAY, PrjStartDt, PrjEndDt))
)

SET DATEFORMAT DMY

INSERT @Sample
(
EmpID,
ProjID,
PrjStartDt,
PrjEndDt
)
VALUES (1, 10, '01/04/2009', '23/04/2009'),
(1, 20, '04/04/2009', '05/06/2009'),
(1, 30, '15/05/2009', '30/09/2009' )

-- Solution starts here
;WITH cteSource
AS (
SELECT s.EmpID,
s.ProjID,
COUNT(*) OVER (PARTITION BY cdiDate) AS Items,
1E + DATEDIFF(DAY, '20090401', '20090930') AS Total
FROM Core.dimDate
LEFT JOIN @Sample AS s ON s.PrjStartDt <= cdiDate
AND s.PrjEndDt >= cdiDate
WHERE cdiDate BETWEEN '20090401' AND '20090930'
), cteAggregation
AS (
SELECT EmpID,
ProjID,
Items,
100E * COUNT(*) / MIN(Items) / MIN(Total) AS Peso
FROM cteSource
GROUP BY EmpID,
ProjID,
Items
)
SELECT EmpID,
ProjID,
SUM(Peso) AS [Weight]
FROM cteAggregation
GROUP BY EmpID,
ProjID
ORDER BY EmpID,
ProjID



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

edumca
Starting Member

3 Posts

Posted - 2011-04-14 : 08:52:27
quote:
Originally posted by Peso

When is EmpID 1 ever working in 3 projects simultaneously?
ProjID 10 ends 23rd of April and Proj30 starts 15th of May...

Anyway, here is a fast solution
DECLARE	@Sample TABLE
(
EmpID INT,
ProjID INT,
PrjStartDt DATE,
PrjEndDt DATE,
Duration AS (1 + DATEDIFF(DAY, PrjStartDt, PrjEndDt))
)

SET DATEFORMAT DMY

INSERT @Sample
(
EmpID,
ProjID,
PrjStartDt,
PrjEndDt
)
VALUES (1, 10, '01/04/2009', '23/04/2009'),
(1, 20, '04/04/2009', '05/06/2009'),
(1, 30, '15/05/2009', '30/09/2009' )

-- Solution starts here
;WITH cteSource
AS (
SELECT s.EmpID,
s.ProjID,
COUNT(*) OVER (PARTITION BY cdiDate) AS Items,
1E + DATEDIFF(DAY, '20090401', '20090930') AS Total
FROM Core.dimDate
LEFT JOIN @Sample AS s ON s.PrjStartDt <= cdiDate
AND s.PrjEndDt >= cdiDate
WHERE cdiDate BETWEEN '20090401' AND '20090930'
), cteAggregation
AS (
SELECT EmpID,
ProjID,
Items,
100E * COUNT(*) / MIN(Items) / MIN(Total) AS Peso
FROM cteSource
GROUP BY EmpID,
ProjID,
Items
)
SELECT EmpID,
ProjID,
SUM(Peso) AS [Weight]
FROM cteAggregation
GROUP BY EmpID,
ProjID
ORDER BY EmpID,
ProjID



N 56°04'39.26"
E 12°55'05.63"




Hi Peso
thnx a tonne for ur reply
can u tell me what do u mean by cdiDate and Core.dimDate
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-04-14 : 15:17:58
It just a tally table with dates, a prebuilt calendar table.
Everyone should have one.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -