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.
| 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 exampleif emp 1 has 3 projects 10,20,30for duration 1/4/09 - 30/4/09 , 10/4/09-25/4/09, 15/4/09 - 30/4/09then weight for project 10 will be (9/30)*100+((5/2)/30)*100+((11/3)/30)*100+((5/2)/30)*100similarly for projects 20 and 30 so that total project weights shld be 100 -------------------------- proj 10 ------------- proj 20 -------------- proj 30I hv tried a lot but can not find suitable solutionprojects assigned can be 1/2/3/........nPls 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? |
 |
|
|
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)/30from projectEmployeeMap mjoin (select map.employeeId, [date], cast(count(*) as money) as projectsCountfrom projectEmployeeMap mapjoin 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 ton t.employeeId = m.employeeIdgroup by m.employeeId, m.projectId MirkoMy blog: http://mirko-marovic-eng.blogspot.com/ |
 |
|
|
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 PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
|
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 231 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/2009so total no of days in this cycle are 183so to calculate the weight for prjid 10i will first find out the no. of days for which this employee has exclusively worked for this project i.e 3 daysno. of days for which the duration of the project 10 is overlapping with other projectsi.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 daysso weight for project 10 is (3/183)*100+((8/3)/183)*100+((20/2)/183)*100similarly v need to calculate the weights for project 20 and 30I hope i m clear now.Please suggest a solution |
 |
|
|
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 solutionDECLARE @Sample TABLE ( EmpID INT, ProjID INT, PrjStartDt DATE, PrjEndDt DATE, Duration AS (1 + DATEDIFF(DAY, PrjStartDt, PrjEndDt)) )SET DATEFORMAT DMYINSERT @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 cteSourceAS ( 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'), cteAggregationAS ( 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 cteAggregationGROUP BY EmpID, ProjIDORDER BY EmpID, ProjID N 56°04'39.26"E 12°55'05.63" |
 |
|
|
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 solutionDECLARE @Sample TABLE ( EmpID INT, ProjID INT, PrjStartDt DATE, PrjEndDt DATE, Duration AS (1 + DATEDIFF(DAY, PrjStartDt, PrjEndDt)) )SET DATEFORMAT DMYINSERT @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 cteSourceAS ( 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'), cteAggregationAS ( 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 cteAggregationGROUP BY EmpID, ProjIDORDER BY EmpID, ProjID N 56°04'39.26"E 12°55'05.63"
Hi Peso thnx a tonne for ur replycan u tell me what do u mean by cdiDate and Core.dimDate |
 |
|
|
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" |
 |
|
|
|
|
|
|
|