| Author |
Topic |
|
njguy
Starting Member
16 Posts |
Posted - 2012-06-18 : 13:47:18
|
| I am having a difficult time on where to start in creating the following view based on two tables.Project Table:ProjectIdMayJunJulAugSepOctNovProjectTasks Table: (Many records for each Project)ProjectTaskIdProjectIdDueDateThe result set would be:Total Sum of the months from the Project TableTotal Count of Projects > system DateTotal Count of Projects < system DateTotal Count of Projects = system Date |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-06-18 : 14:49:28
|
| [code]select m.ProjectId,SUM(val) as Total,SUM(CASE WHEN MONTH(DueDate) < MonthNo THEN m.val END) AS FutureTotal,SUM(CASE WHEN MONTH(DueDate) > MonthNo THEN m.val END) AS PrevTotal,SUM(CASE WHEN MONTH(DueDate) = MonthNo THEN m.val END) AS CurrentTotalfrom(select ProjectId,CASE monthnameWHEN 'Jan' THEN 1WHEN 'Feb' THEN 2...WHEN 'Dec' THEN 12END AS MonthNo,valfrom Project punpivot(val for monthname in (May,Jun,Jul,Aug,Sep,Oct,Nov))u)minner join ProjectTasks pton pt.ProjectId = m.ProjectIdgroup by m.ProjectId[/code]i assume you've only singe year worth of data in Project else you might have to include year field also in it------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
njguy
Starting Member
16 Posts |
Posted - 2012-06-18 : 15:02:52
|
| My bad on explaing the result set ....it should be The result set would be:Total Sum of the months from the Project TableTotal Count of ProjectTasks > system Date (From Task Table)Total Count of ProjectsTasks < system Date (From Task Table)Total Count of ProjectsTaks = system Date (From Task Table) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
njguy
Starting Member
16 Posts |
Posted - 2012-06-19 : 09:09:36
|
| Project Table:ProjectIdMayJunJulAugSepOctNov....ProjectTasks Table: (Many records for each Project)ProjectTaskIdProjectIdDueDateselect p.projectid, sum(p.may + p.jun + p.jul + p.aug + p.sep + p.oct + p.nov + p.dec + p.jan + p.feb + p.mar + p.apr) as totalfrom projects pleft outer join projecttasks pton pt.projectid = p.projectidgroup by p.projectidThe above gets me the totals I am looking for the second part I need is to count the number of tasks for each project and categorize them based on the system dateTotal Count of Project tasks(duedate) > system DateTotal Count of Project tasks(duedate) < system DateTotal Count of Project tasks(duedate) = system Date |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-06-19 : 14:17:43
|
| thats what i gave suggestion. try it first and see what it gives. then join that query onto your current query to get counts merged with this------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
njguy
Starting Member
16 Posts |
Posted - 2012-06-19 : 22:19:16
|
| I ran the following:select m.ProjectId,SUM(val) as Total,SUM(CASE WHEN MONTH(DueDate) < MonthNo THEN m.val END) AS FutureTotal,SUM(CASE WHEN MONTH(DueDate) > MonthNo THEN m.val END) AS PrevTotal,SUM(CASE WHEN MONTH(DueDate) = MonthNo THEN m.val END) AS CurrentTotalfrom(select ProjectId,CASE monthnameWHEN 'Jan' THEN 1WHEN 'Feb' THEN 2WHEN 'Mar' THEN 3WHEN 'Apr' THEN 4WHEN 'May' THEN 5WHEN 'Jun' THEN 6WHEN 'Jul' THEN 7WHEN 'Aug' THEN 8WHEN 'Sep' THEN 9WHEN 'Oct' THEN 10WHEN 'Nov' THEN 11WHEN 'Dec' THEN 12END AS MonthNo,valfrom Projects punpivot(val for monthname in (May,Jun,Jul,Aug,Sep,Oct,Nov,Dec,Jan,Feb,Mar,Apr))u)mleft outer join ProjectTasks pton pt.ProjectId = m.ProjectIdgroup by m.ProjectIdSample result set:ProjectId Total FutureTotal PrevTotal CurrentTotal1 900.00 300.00 300.00 300.00What I was looking for was a count based on the followingTotal Count of Project tasks that have a duedate > current system DateTotal Count of Project tasks that have a duedate < current system DateTotal Count of Project tasks that have a duedate = current system Dateso based on this projecttask table ProjectTaskId ProjectId ActionItem DueDate37 1 action1 2012-06-19 38 1 action2 2012-06-01 39 1 action3 2012-06-26 39 1 action3 2012-06-19 the results should have beenProjectId Total FutureDateCount PrevDateCount CurrentDateCount1 900.00 1 1 2 |
 |
|
|
vinu.vijayan
Posting Yak Master
227 Posts |
Posted - 2012-06-20 : 06:41:54
|
quote: Originally posted by njguy My bad on explaing the result set ....it should be The result set would be:Total Sum of the months from the Project TableTotal Count of ProjectTasks > system Date (From Task Table)Total Count of ProjectsTasks < system Date (From Task Table)Total Count of ProjectsTaks = system Date (From Task Table)
Its still your bad mate.If you still haven't got it working then it means that you forgot to give enough info.If you post the DDL of the tables and some sample Data to go with it then you can get faster and better solutions.N 28° 33' 11.93148"E 77° 14' 33.66384" |
 |
|
|
njguy
Starting Member
16 Posts |
Posted - 2012-06-20 : 10:04:22
|
| The last message I sent gave data examples with results |
 |
|
|
njguy
Starting Member
16 Posts |
Posted - 2012-06-20 : 13:24:53
|
| The following accomplished what I needed....select p.projectid, sum(p.may + p.jun + p.jul + p.aug + p.sep + p.oct + p.nov + p.dec + p.jan + p.feb + p.mar + p.apr) as total,COUNT(CASE WHEN pt.DueDate > CONVERT(date,GETDATE()) THEN pt.ProjectId END) AS TotalLate,COUNT(CASE WHEN pt.DueDate < CONVERT(date,GETDATE()) THEN pt.ProjectId END) AS TotalOnTime,COUNT(CASE WHEN pt.DueDate = CONVERT(date,GETDATE()) THEN pt.ProjectId END) AS TotalCurrent,CONVERT(date,GETDATE()) as currentdatefrom projects pleft outer join projecttasks pton pt.projectid = p.projectidgroup by p.projectid |
 |
|
|
|