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
 subquery the right solution?

Author  Topic 

dlorenc
Posting Yak Master

172 Posts

Posted - 2010-04-22 : 17:28:25
I am trying to pull three fields from an aggregate table. Should I use a subquery? see the red line below...

basically the aggregate table has one record per month, the field I want to pull is capacity...the select will need to match the 'Month' alias in the 'red' case statement below, with the month field in the aggregate table....so this is like a lookup...


this is a view...

SELECT TOP (100) PERCENT a.wcm_id,a.project_id, a.datamartdate, a.plannedenddate,a.actualenddate, b.projectstate
,CASE WHEN a.actualenddate IS NULL
THEN DATEADD(mm, DATEDIFF(mm, 0, a.plannedenddate), 0)
Else DATEADD(mm, DATEDIFF(mm, 0, a.actualenddate), 0)
END AS Month

,CASE WHEN b.projectstate IN ('Active')
THEN 1
ELSE NULL
END AS Due
,CASE WHEN a.plannedenddate IS NOT NULL AND a.actualenddate IS NOT NULL
THEN CASE WHEN datediff(hh, a.PlannedEndDate, a.ActualEndDate) <= 0
THEN 1 ELSE NULL
END
END AS Ontime
,CASE WHEN a.plannedenddate IS NOT NULL AND a.actualenddate IS NOT NULL
THEN CASE WHEN datediff(hh, a.PlannedEndDate, a.ActualEndDate)> 0
THEN 1
ELSE NULL
END
END AS Late
,a.OBS, a.PIN, b.ProjectTitle, a.Capability, b.ManagementLead, a.WorkCoordinator, b.ReportToLeadership,a.PlannedEndDate, GETDATE() AS Entered, a.DataMartDate
,Capacity = (select Capacity from EPM_Milestone_performance_Aggregate where month=month)

FROM dbo.EPM_WCMilestones AS a
JOIN dbo.EPM_Project AS b ON a.Project_ID = b.Project_ID

WHERE a.PlannedEndDate IS NOT NULL and b.projectstate not in ('Inactive','Cancelled')
and b.datamartdate = (select top 1 datamartdate from epm_project order by datamartdate desc)
and a.datamartdate = (select top 1 datamartdate from epm_wcmilestones order by datamartdate desc)

ORDER BY wcm_id

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-22 : 18:34:26
How about this:
SELECT TOP (100) PERCENT z.*, c.Capacity
FROM (
SELECT a.wcm_id,a.project_id, a.datamartdate, a.plannedenddate,a.actualenddate, b.projectstate
,CASE WHEN a.actualenddate IS NULL
THEN DATEADD(mm, DATEDIFF(mm, 0, a.plannedenddate), 0)
Else DATEADD(mm, DATEDIFF(mm, 0, a.actualenddate), 0)
END AS Month
,CASE WHEN b.projectstate IN ('Active')
THEN 1
ELSE NULL
END AS Due
,CASE WHEN a.plannedenddate IS NOT NULL AND a.actualenddate IS NOT NULL
THEN CASE WHEN datediff(hh, a.PlannedEndDate, a.ActualEndDate) <= 0
THEN 1 ELSE NULL
END
END AS Ontime
,CASE WHEN a.plannedenddate IS NOT NULL AND a.actualenddate IS NOT NULL
THEN CASE WHEN datediff(hh, a.PlannedEndDate, a.ActualEndDate)> 0
THEN 1
ELSE NULL
END
END AS Late
,a.OBS, a.PIN, b.ProjectTitle, a.Capability, b.ManagementLead, a.WorkCoordinator,
b.ReportToLeadership,a.PlannedEndDate, GETDATE() AS Entered, a.DataMartDate

FROM dbo.EPM_WCMilestones AS a
JOIN dbo.EPM_Project AS b ON a.Project_ID = b.Project_ID

WHERE a.PlannedEndDate IS NOT NULL and b.projectstate not in ('Inactive','Cancelled')
and b.datamartdate = (select top 1 datamartdate from epm_project order by datamartdate desc)
and a.datamartdate = (select top 1 datamartdate from epm_wcmilestones order by datamartdate desc)
) z
LEFT JOIN EPM_Milestone_performance_Aggregate c
ON z.month = c.month

ORDER BY wcm_id


------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

dlorenc
Posting Yak Master

172 Posts

Posted - 2010-04-23 : 09:30:54
I see the method... pretty slick... but got one error..

Msg 8156, Level 16, State 1, Line 1
The column 'PlannedEndDate' was specified multiple times for 'z'.

any ideas?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-23 : 09:34:03
remove the second occurance of a.plannedenddate inside z derived table after b.ReportToLeadership

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-04-23 : 09:34:50
quote:
Originally posted by dlorenc

I see the method... pretty slick... but got one error..

Msg 8156, Level 16, State 1, Line 1
The column 'PlannedEndDate' was specified multiple times for 'z'.

any ideas?


Remove a.PlannedEndDate from the first line of the code

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

dlorenc
Posting Yak Master

172 Posts

Posted - 2010-04-23 : 10:10:53
ahhh...I see now..the double entry of the field...!...
DBA-ITM - excellent solution...

visakh..madhuanan...thank you for the troubleshooting!...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-23 : 10:13:07
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

dlorenc
Posting Yak Master

172 Posts

Posted - 2010-04-23 : 10:17:52
spoke too soon..grrr...I swear, it worked by removing those fields...

but when I saved it to a view..and tried to open it....I got this error..

Msg 4429, Level 16, State 1, Line 1
View or function 'dbo.EPM_Milestone_Performance_Aggregate' contains a self-reference. Views or functions cannot reference themselves directly or indirectly.
Msg 4413, Level 16, State 1, Line 1
Could not use view or function 'EPM_Milestone_performance_Aggregate' because of binding errors.
Go to Top of Page

dlorenc
Posting Yak Master

172 Posts

Posted - 2010-04-23 : 10:19:04
mm... something happened to the aggregate view...I must have over written it when I was saving....

be right back..
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-24 : 15:02:20
quote:
Originally posted by dlorenc

spoke too soon..grrr...I swear, it worked by removing those fields...

but when I saved it to a view..and tried to open it....I got this error..

Msg 4429, Level 16, State 1, Line 1
View or function 'dbo.EPM_Milestone_Performance_Aggregate' contains a self-reference. Views or functions cannot reference themselves directly or indirectly.
Msg 4413, Level 16, State 1, Line 1
Could not use view or function 'EPM_Milestone_performance_Aggregate' because of binding errors.



The select query selects from dbo.EPM_Milestone_Performance_Aggregate, so if you're trying to creates a view from the select query, then you can't name the view dbo.EPM_Milestone_Performance_Aggregate.

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

dlorenc
Posting Yak Master

172 Posts

Posted - 2010-04-26 : 17:40:30
got it..thank you....kinna like a circular calculation in excel..mmm?..

I created view compare1 -> (that is used) to produce monthly aggregates -> compare (which includes the monthly capacity numbers for reporting..
Go to Top of Page
   

- Advertisement -