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 |
|
srucker
Starting Member
26 Posts |
Posted - 2008-06-18 : 12:58:11
|
| I need help from you guru's. I have this sp which normailzes some data to a temp table. I need to extend the "return results" select statement to join and get more data.So I add task_charge_type to the select and created 3 joins to get it all together.I can execute the alter sp command with no errors; however when I try to execute the sp I get the following errors:Msg 209, Level 16, State 1, Procedure sp_psactualsbyweek, Line 69Ambiguous column name 'PROJ_NAME'.Msg 209, Level 16, State 1, Procedure sp_psactualsbyweek, Line 69Ambiguous column name 'PROJ_NAME'.set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo-- sp_psactualsbyweek.sql---- This stored procedure generates a report of the actual hours worked by a-- resource by project and task for a specific date range from the table-- MSP_WEB_WORK.ALTER procedure [dbo].[sp_psactualsbyweek] -- date range variables @startdate datetime, @enddate datetimeas-- cursor variablesdeclare @res_name nvarchar(510)declare @proj_name nvarchar(510)declare @task_name nvarchar(510)declare @start datetimedeclare @finish datetimedeclare @value decimal(25,6)declare @days intdeclare @count intdeclare @date datetime-- temp table to hold normalized datacreate table #actuals_data ( RES_NAME nvarchar(510), PROJ_NAME nvarchar(510), TASK_NAME nvarchar(510), WORK_DAY datetime, HOURS decimal(25,6) )-- cursor to convert denormalized datadeclare actuals_csr cursor forSELECT DISTINCT r.RES_NAME, p.PROJ_NAME, a.TASK_NAME, w.WWORK_START, w.WWORK_FINISH, w.WWORK_VALUEFROM MSP_WEB_RESOURCES AS r INNER JOIN MSP_WEB_ASSIGNMENTS AS a ON a.WRES_ID = r.WRES_IDINNER JOIN MSP_WEB_WORK AS w ON w.WASSN_ID = a.WASSN_IDINNER JOIN MSP_WEB_PROJECTS AS p ON p.WPROJ_ID = a.WPROJ_IDwhere w.WWORK_TYPE = 1 -- actual workand ((w.WWORK_START between @startdate and @enddate) or (w.WWORK_FINISH between @startdate and @enddate))order by 1, 2, 3, 4open actuals_csrfetch next from actuals_csr into @res_name, @proj_name, @task_name, @start, @finish, @valuewhile @@fetch_status = 0begin select @days = DATEDIFF(day, @start, @finish) + 1 select @count = 0 while @count < @days begin select @date = DATEADD(day ,@count, @start) insert into #actuals_data values( @res_name, @proj_name, @task_name, @date, ((@value/1000)/60) ) select @count = @count + 1 end fetch next from actuals_csr into @res_name, @proj_name, @task_name, @start, @finish, @valueendclose actuals_csrdeallocate actuals_csr-- return resultsselect RES_NAME, PROJ_NAME, Task_Name, Task_Charge_Type = case when pe.ProjectEnterpriseOutlineCode30ID = 530 Then 'Expensed' when pe.ProjectEnterpriseOutlineCode30ID = 529 and te.TaskEnterpriseOutlineCode30ID = 527 Then 'Capital' end, Work_Day, sum(HOURS) as 'TOTAL HOURS'from #actuals_dataINNER JOIN MSP_WEB_PROJECTS AS p on #actuals_data.PROJ_NAME = p.PROJ_NAMEINNER JOIN MSP_VIEW_PROJ_PROJECTS_ENT AS pe ON pe.WPROJ_ID = p.WPROJ_IDINNER JOIN MSP_VIEW_PROJ_TASKS_ENT AS te ON te.WPROJ_ID = pe.WPROJ_IDwhere WORK_DAY between @startdate and @enddategroup by RES_NAME, PROJ_NAME, Task_Name, Work_Dayorder by res_namedrop table #actuals_data--Msg 209, Level 16, State 1, Procedure sp_psactualsbyweek, Line 69--Ambiguous column name 'PROJ_NAME'.--Msg 209, Level 16, State 1, Procedure sp_psactualsbyweek, Line 69--Ambiguous column name 'PROJ_NAME'. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-18 : 13:01:10
|
| You need to qualify the table alias name on that column in the SELECT statementMadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-18 : 13:03:47
|
quote: Originally posted by srucker I need help from you guru's. I have this sp which normailzes some data to a temp table. I need to extend the "return results" select statement to join and get more data.So I add task_charge_type to the select and created 3 joins to get it all together.I can execute the alter sp command with no errors; however when I try to execute the sp I get the following errors:Msg 209, Level 16, State 1, Procedure sp_psactualsbyweek, Line 69Ambiguous column name 'PROJ_NAME'.Msg 209, Level 16, State 1, Procedure sp_psactualsbyweek, Line 69Ambiguous column name 'PROJ_NAME'.set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo-- sp_psactualsbyweek.sql---- This stored procedure generates a report of the actual hours worked by a-- resource by project and task for a specific date range from the table-- MSP_WEB_WORK.ALTER procedure [dbo].[sp_psactualsbyweek] -- date range variables @startdate datetime, @enddate datetimeas-- cursor variablesdeclare @res_name nvarchar(510)declare @proj_name nvarchar(510)declare @task_name nvarchar(510)declare @start datetimedeclare @finish datetimedeclare @value decimal(25,6)declare @days intdeclare @count intdeclare @date datetime-- temp table to hold normalized datacreate table #actuals_data ( RES_NAME nvarchar(510), PROJ_NAME nvarchar(510), TASK_NAME nvarchar(510), WORK_DAY datetime, HOURS decimal(25,6) )-- cursor to convert denormalized datadeclare actuals_csr cursor forSELECT DISTINCT r.RES_NAME, p.PROJ_NAME, a.TASK_NAME, w.WWORK_START, w.WWORK_FINISH, w.WWORK_VALUEFROM MSP_WEB_RESOURCES AS r INNER JOIN MSP_WEB_ASSIGNMENTS AS a ON a.WRES_ID = r.WRES_IDINNER JOIN MSP_WEB_WORK AS w ON w.WASSN_ID = a.WASSN_IDINNER JOIN MSP_WEB_PROJECTS AS p ON p.WPROJ_ID = a.WPROJ_IDwhere w.WWORK_TYPE = 1 -- actual workand ((w.WWORK_START between @startdate and @enddate) or (w.WWORK_FINISH between @startdate and @enddate))order by 1, 2, 3, 4open actuals_csrfetch next from actuals_csr into @res_name, @proj_name, @task_name, @start, @finish, @valuewhile @@fetch_status = 0begin select @days = DATEDIFF(day, @start, @finish) + 1 select @count = 0 while @count < @days begin select @date = DATEADD(day ,@count, @start) insert into #actuals_data values( @res_name, @proj_name, @task_name, @date, ((@value/1000)/60) ) select @count = @count + 1 end fetch next from actuals_csr into @res_name, @proj_name, @task_name, @start, @finish, @valueendclose actuals_csrdeallocate actuals_csr-- return resultsselect RES_NAME, {p/pe/te}.PROJ_NAME, Task_Name, Task_Charge_Type = case when pe.ProjectEnterpriseOutlineCode30ID = 530 Then 'Expensed' when pe.ProjectEnterpriseOutlineCode30ID = 529 and te.TaskEnterpriseOutlineCode30ID = 527 Then 'Capital' end, Work_Day, sum(HOURS) as 'TOTAL HOURS'from #actuals_dataINNER JOIN MSP_WEB_PROJECTS AS p on #actuals_data.PROJ_NAME = p.PROJ_NAMEINNER JOIN MSP_VIEW_PROJ_PROJECTS_ENT AS pe ON pe.WPROJ_ID = p.WPROJ_IDINNER JOIN MSP_VIEW_PROJ_TASKS_ENT AS te ON te.WPROJ_ID = pe.WPROJ_IDwhere WORK_DAY between @startdate and @enddategroup by RES_NAME, {p/pe/te}.PROJ_NAME, Task_Name, Work_Dayorder by res_namedrop table #actuals_data--Msg 209, Level 16, State 1, Procedure sp_psactualsbyweek, Line 69--Ambiguous column name 'PROJ_NAME'.--Msg 209, Level 16, State 1, Procedure sp_psactualsbyweek, Line 69--Ambiguous column name 'PROJ_NAME'.
You have column PROJ_NAME in more than one table so please fully qualify it by giving the table alias where you're using it (i dont know which table has it think its p) |
 |
|
|
srucker
Starting Member
26 Posts |
Posted - 2008-06-18 : 13:18:43
|
| OK Got it! Thanks.So now I have another issue where I wrote a case staement and now need to see how I can add it to the group by clause since everything in the select has to be in the group by.Here is the error I get from executing the SP below:--Msg 329, Level 16, State 1, Procedure sp_psactualsbyweek, Line 69--Each GROUP BY expression must contain at least one column reference.set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo-- sp_psactualsbyweek.sql---- This stored procedure generates a report of the actual hours worked by a-- resource by project and task for a specific date range from the table-- MSP_WEB_WORK.ALTER procedure [dbo].[sp_psactualsbyweek] -- date range variables @startdate datetime, @enddate datetimeas-- cursor variablesdeclare @res_name nvarchar(510)declare @proj_name nvarchar(510)declare @task_name nvarchar(510)declare @start datetimedeclare @finish datetimedeclare @value decimal(25,6)declare @days intdeclare @count intdeclare @date datetime-- temp table to hold normalized datacreate table #actuals_data ( RES_NAME nvarchar(510), PROJ_NAME nvarchar(510), TASK_NAME nvarchar(510), WORK_DAY datetime, HOURS decimal(25,6) )-- cursor to convert denormalized datadeclare actuals_csr cursor forSELECT DISTINCT r.RES_NAME, p.PROJ_NAME, a.TASK_NAME, w.WWORK_START, w.WWORK_FINISH, w.WWORK_VALUEFROM MSP_WEB_RESOURCES AS r INNER JOIN MSP_WEB_ASSIGNMENTS AS a ON a.WRES_ID = r.WRES_IDINNER JOIN MSP_WEB_WORK AS w ON w.WASSN_ID = a.WASSN_IDINNER JOIN MSP_WEB_PROJECTS AS p ON p.WPROJ_ID = a.WPROJ_IDwhere w.WWORK_TYPE = 1 -- actual workand ((w.WWORK_START between @startdate and @enddate) or (w.WWORK_FINISH between @startdate and @enddate))order by 1, 2, 3, 4open actuals_csrfetch next from actuals_csr into @res_name, @proj_name, @task_name, @start, @finish, @valuewhile @@fetch_status = 0begin select @days = DATEDIFF(day, @start, @finish) + 1 select @count = 0 while @count < @days begin select @date = DATEADD(day ,@count, @start) insert into #actuals_data values( @res_name, @proj_name, @task_name, @date, ((@value/1000)/60) ) select @count = @count + 1 end fetch next from actuals_csr into @res_name, @proj_name, @task_name, @start, @finish, @valueendclose actuals_csrdeallocate actuals_csr-- return resultsselect RES_NAME, p.PROJ_NAME, Task_Name, 'Task_Charge_Type' = case when pe.ProjectEnterpriseOutlineCode30ID = 530 Then 'Expensed' when pe.ProjectEnterpriseOutlineCode30ID = 529 and te.TaskEnterpriseOutlineCode30ID = 527 Then 'Capital' end, a.Work_Day, sum(a.HOURS) as 'TOTAL HOURS'from #actuals_data as a, MSP_WEB_PROJECTS AS p --on #actuals_data.PROJ_NAME = p.PROJ_NAMEINNER JOIN MSP_VIEW_PROJ_PROJECTS_ENT AS pe ON pe.WPROJ_ID = p.WPROJ_IDINNER JOIN MSP_VIEW_PROJ_TASKS_ENT AS te ON te.WPROJ_ID = pe.WPROJ_IDwhere WORK_DAY between @startdate and @enddategroup by RES_NAME, p.PROJ_NAME, Task_Name, 'Task_Charge_Type', Work_Dayorder by res_namedrop table #actuals_data |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-18 : 13:50:26
|
Make it like this & tryselect * FROM(select RES_NAME, p.PROJ_NAME, Task_Name, 'Task_Charge_Type' = case when pe.ProjectEnterpriseOutlineCode30ID = 530 Then 'Expensed' when pe.ProjectEnterpriseOutlineCode30ID = 529 and te.TaskEnterpriseOutlineCode30ID = 527 Then 'Capital' end, a.Work_Day, sum(a.HOURS) as 'TOTAL HOURS'from #actuals_data as a,MSP_WEB_PROJECTS AS p --on #actuals_data.PROJ_NAME = p.PROJ_NAMEINNER JOIN MSP_VIEW_PROJ_PROJECTS_ENT AS pe ON pe.WPROJ_ID = p.WPROJ_IDINNER JOIN MSP_VIEW_PROJ_TASKS_ENT AS te ON te.WPROJ_ID = pe.WPROJ_IDwhere WORK_DAY between @startdate and @enddate)tgroup by RES_NAME, p.PROJ_NAME, Task_Name, Task_Charge_Type, Work_Dayorder by res_name |
 |
|
|
srucker
Starting Member
26 Posts |
Posted - 2008-06-18 : 14:40:27
|
| Here is what I get from executing the updated sp:--Msg 8120, Level 16, State 1, Procedure sp_psactualsbyweek, Line 70--Column '#actuals_data.RES_NAME' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.--Msg 4104, Level 16, State 1, Procedure sp_psactualsbyweek, Line 70--The multi-part identifier "p.PROJ_NAME" could not be bound.set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo-- sp_psactualsbyweek.sql---- This stored procedure generates a report of the actual hours worked by a-- resource by project and task for a specific date range from the table-- MSP_WEB_WORK.ALTER procedure [dbo].[sp_psactualsbyweek] -- date range variables @startdate datetime, @enddate datetimeas-- cursor variablesdeclare @res_name nvarchar(510)declare @proj_name nvarchar(510)declare @task_name nvarchar(510)declare @start datetimedeclare @finish datetimedeclare @value decimal(25,6)declare @days intdeclare @count intdeclare @date datetime-- temp table to hold normalized datacreate table #actuals_data ( RES_NAME nvarchar(510), PROJ_NAME nvarchar(510), TASK_NAME nvarchar(510), WORK_DAY datetime, HOURS decimal(25,6) )-- cursor to convert denormalized datadeclare actuals_csr cursor forSELECT DISTINCT r.RES_NAME, p.PROJ_NAME, a.TASK_NAME, w.WWORK_START, w.WWORK_FINISH, w.WWORK_VALUEFROM MSP_WEB_RESOURCES AS r INNER JOIN MSP_WEB_ASSIGNMENTS AS a ON a.WRES_ID = r.WRES_IDINNER JOIN MSP_WEB_WORK AS w ON w.WASSN_ID = a.WASSN_IDINNER JOIN MSP_WEB_PROJECTS AS p ON p.WPROJ_ID = a.WPROJ_IDwhere w.WWORK_TYPE = 1 -- actual workand ((w.WWORK_START between @startdate and @enddate) or (w.WWORK_FINISH between @startdate and @enddate))order by 1, 2, 3, 4open actuals_csrfetch next from actuals_csr into @res_name, @proj_name, @task_name, @start, @finish, @valuewhile @@fetch_status = 0begin select @days = DATEDIFF(day, @start, @finish) + 1 select @count = 0 while @count < @days begin select @date = DATEADD(day ,@count, @start) insert into #actuals_data values( @res_name, @proj_name, @task_name, @date, ((@value/1000)/60) ) select @count = @count + 1 end fetch next from actuals_csr into @res_name, @proj_name, @task_name, @start, @finish, @valueendclose actuals_csrdeallocate actuals_csr-- return resultsselect * FROM(select RES_NAME, p.PROJ_NAME, Task_Name, 'Task_Charge_Type' = case when pe.ProjectEnterpriseOutlineCode30ID = 530 Then 'Expensed' when pe.ProjectEnterpriseOutlineCode30ID = 529 and te.TaskEnterpriseOutlineCode30ID = 527 Then 'Capital' end, a.Work_Day, sum(a.HOURS) as 'TOTAL HOURS'from #actuals_data as a, MSP_WEB_PROJECTS AS p INNER JOIN MSP_VIEW_PROJ_PROJECTS_ENT AS pe ON pe.WPROJ_ID = p.WPROJ_IDINNER JOIN MSP_VIEW_PROJ_TASKS_ENT AS te ON te.WPROJ_ID = pe.WPROJ_IDwhere WORK_DAY between @startdate and @enddate)tgroup by RES_NAME, p.PROJ_NAME, Task_Name, Task_Charge_Type, Work_Dayorder by res_namedrop table #actuals_data |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-18 : 14:55:35
|
Give aliases to all columns. ALso you've a ON conditionfor join missing.select t.RES_NAME, t.PROJ_NAME, t.Task_Name, t.Task_Charge_Type, t.Work_Day, t.TOTAL HOURS FROM(select RES_NAME, p.PROJ_NAME, Task_Name, 'Task_Charge_Type' = case when pe.ProjectEnterpriseOutlineCode30ID = 530 Then 'Expensed' when pe.ProjectEnterpriseOutlineCode30ID = 529 and te.TaskEnterpriseOutlineCode30ID = 527 Then 'Capital' end, a.Work_Day, sum(a.HOURS) as 'TOTAL HOURS'from #actuals_data as aINNER JOIN MSP_WEB_PROJECTS AS p ON conditionINNER JOIN MSP_VIEW_PROJ_PROJECTS_ENT AS pe ON pe.WPROJ_ID = p.WPROJ_IDINNER JOIN MSP_VIEW_PROJ_TASKS_ENT AS te ON te.WPROJ_ID = pe.WPROJ_IDwhere WORK_DAY between @startdate and @enddate)tgroup by t.RES_NAME, t.PROJ_NAME, t.Task_Name, t.Task_Charge_Type, t.Work_Dayorder by t.res_name |
 |
|
|
|
|
|
|
|