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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Adding a new column issue?

Author  Topic 

ostinoh
Yak Posting Veteran

66 Posts

Posted - 2011-12-06 : 13:01:05
Hello -

I'm need of help figuring out how to add a new column called TASK to the following script. Currently when I run these there are two columns that come up named Resource_Name and Business_Unit which is right.

Here is column name the from the script below that I need added AND (cd3.Description = 'Paid Time Off' OR t.name = 'Holiday')

SELECT r.name as Resource_Name,rw.name as Business_Unit
FROM Resources r WITH(NOLOCK)
INNER JOIN Workgroupmember wgm WITH(NOLOCK) ON wgm.ResourceID = R.ResourceID and wgm.historical = 0
INNER JOIN Workgroup rw WITH(NOLOCK) ON rw.workgroupid = wgm.workgroupid
WHERE r.deleted = 0
AND r.name NOT LIKE '(C)%'
AND r.name NOT LIKE '(I)%'
AND r.name NOT LIKE '(Gen%'
AND r.name NOT LIKE '(Con%'
AND r.name NOT LIKE 'ZZ%'
AND r.name NOT BETWEEN '1/%' AND '12/31/%'
AND rw.workgrouptypeid <> '3B08677-715C-459-A88-0C642FF263'
AND rw.workgroupid <> '1B17927-DF2-449-9E3-C5CDD736A4'
AND NOT EXISTS (
SELECT e.name as Engagement,ew.name as Workgroup,p.name as Project,t.name as Task,rs.name as Resource_Name,rw.name as Business_Unit,*
FROM TaskAssignment ta
INNER JOIN Tasks t WITH(NOLOCK) ON t.taskid = ta.taskid
INNER JOIN Resources rs WITH(NOLOCK) ON rs.Resourceid = ta.Resourceid
INNER JOIN Workgroupmember wgm WITH(NOLOCK) ON wgm.ResourceID = RS.ResourceID and wgm.historical = 0
INNER JOIN Workgroup rw WITH(NOLOCK) ON rw.workgroupid = wgm.workgroupid
INNER JOIN Project p WITH(NOLOCK) ON p.Projectid = t.projectid
INNER JOIN Engagement e WITH(NOLOCK) ON p.Engagementid = e.Engagementid
INNER JOIN Workgroup ew WITH(NOLOCK) ON ew.workgroupid = e.Associatedworkgroup
INNER JOIN UDFCode AS udf2 WITH(NOLOCK) ON udf2.EntityId = t.TaskId AND udf2.ItemName = 'TaskCode1'
INNER JOIN CodeDetail AS cd3 WITH(NOLOCK) ON cd3.CodeDetail = udf2.UDFCode
AND (cd3.Description = 'Paid Time Off' OR t.name = 'Holiday')
AND t.locked = 0
AND rs.resourceid = r.resourceid
AND e.Associatedworkgroup = rw.workgroupid)

UNION

SELECT r.name as Resource_Name,rw.name as Business_Unit
FROM Resources r WITH(NOLOCK)
INNER JOIN Workgroupmember wgm WITH(NOLOCK) ON wgm.ResourceID = R.ResourceID and wgm.historical = 0
INNER JOIN Workgroup rw WITH(NOLOCK) ON rw.workgroupid = wgm.workgroupid
WHERE r.deleted = 0
AND r.name NOT LIKE '(C)%'
AND r.name NOT LIKE '(I)%'
AND r.name NOT LIKE '(Gen%'
AND r.name NOT LIKE '(Con%'
AND r.name NOT LIKE 'ZZ_%'
AND r.name NOT BETWEEN '1/%' AND '12/31/%'
AND rw.workgrouptypeid = '3B08067-715-459-A88-0C5642FF263'
AND NOT EXISTS (
SELECT e.name as Engagement,ew.name as Workgroup,p.name as Project,t.name as Task,rs.name as Resource_Name,rw.name as Business_Unit,*
FROM TaskAssignment ta
INNER JOIN Tasks t WITH(NOLOCK) ON t.taskid = ta.taskid
INNER JOIN Resources rs WITH(NOLOCK) ON rs.Resourceid = ta.Resourceid
INNER JOIN Workgroupmember wgm WITH(NOLOCK) ON wgm.ResourceID = RS.ResourceID and wgm.historical = 0
INNER JOIN Workgroup rw WITH(NOLOCK) ON rw.workgroupid = wgm.workgroupid
INNER JOIN Project p WITH(NOLOCK) ON p.Projectid = t.projectid
INNER JOIN Engagement e WITH(NOLOCK) ON p.Engagementid = e.Engagementid
INNER JOIN Workgroup ew WITH(NOLOCK) ON ew.workgroupid = e.Associatedworkgroup
INNER JOIN UDFCode AS udf2 WITH(NOLOCK) ON udf2.EntityId = t.TaskId AND udf2.ItemName = 'TaskCode1'
INNER JOIN CodeDetail AS cd3 WITH(NOLOCK) ON cd3.CodeDetail = udf2.UDFCode
AND (cd3.Description = 'Paid Time Off' OR t.name = 'Holiday')
AND p.name = 'RMG - CSG Time Off'
AND t.locked = 0
AND rs.resourceid = r.resourceid)

UNION

SELECT r.name as Resource_Name,rw.name as Business_Unit
FROM Resources r WITH(NOLOCK)
INNER JOIN Workgroupmember wgm WITH(NOLOCK) ON wgm.ResourceID = R.ResourceID and wgm.historical = 0
INNER JOIN Workgroup rw WITH(NOLOCK) ON rw.workgroupid = wgm.workgroupid
WHERE r.deleted = 0
AND r.name NOT LIKE '(C)%'
AND r.name NOT LIKE '(I)%'
AND r.name NOT LIKE '(Gen%'
AND r.name NOT LIKE '(Con%'
AND r.name NOT LIKE 'ZZ_%'
AND r.name NOT BETWEEN '1/%' AND '12/31/%'
AND rw.workgroupid = '1B17967-D62-44B9-9E3-C5C8DD7D6A4'
AND rw.WorkgroupId = 'B4B23EA-F912-4890-836-B40942D30B0'
AND NOT EXISTS (
SELECT e.name as Engagement,ew.name as Workgroup,p.name as Project,t.name as Task,rs.name as Resource_Name,rw.name as Business_Unit,*
FROM TaskAssignment ta
INNER JOIN Tasks t WITH(NOLOCK) ON t.taskid = ta.taskid
INNER JOIN Resources rs WITH(NOLOCK) ON rs.Resourceid = ta.Resourceid
INNER JOIN Workgroupmember wgm WITH(NOLOCK) ON wgm.ResourceID = RS.ResourceID and wgm.historical = 0
INNER JOIN Workgroup rw WITH(NOLOCK) ON rw.workgroupid = wgm.workgroupid
INNER JOIN Project p WITH(NOLOCK) ON p.Projectid = t.projectid
INNER JOIN Engagement e WITH(NOLOCK) ON p.Engagementid = e.Engagementid
INNER JOIN Workgroup ew WITH(NOLOCK) ON ew.workgroupid = e.Associatedworkgroup
INNER JOIN UDFCode AS udf2 WITH(NOLOCK) ON udf2.EntityId = t.TaskId AND udf2.ItemName = 'TaskCode1'
INNER JOIN CodeDetail AS cd3 WITH(NOLOCK) ON cd3.CodeDetail = udf2.UDFCode
AND (cd3.Description = 'Paid Time Off' OR t.name = 'Holiday')
AND p.name = 'RMG - CCX Time Off'
AND t.locked = 0
AND rs.resourceid = r.resourceid)

Thanks in advance,
D

ostinoh
Yak Posting Veteran

66 Posts

Posted - 2011-12-06 : 13:42:31
Sorry after rereading my post I realized that I put in the wrong Table name I need to add. I need to add a Column name TASK ID pulled from the table name TASK. Here is an example from the table. TaskID 33466396-8AAF-48A6-985D-005C3AE36963 Name Holiday when I try to add the task name to the first Select I get nothing back and when I add to the second Select I see it but no data comes back.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-07 : 00:00:09
you've not provided enough info for us to help on. Your table task is not even included in main query so you need to add a new join to it. But for that we need to know how it is related to any one or more of other tables in query. Otherwise it will end up doing a cross join which is certainly not what you want. also we need some details on how relationship is from existing tables to task table ie is it one to one, one to many etc.
the easiest thing for you as well as for anyone helping you would be put the question in following format. this will make sure we understand your scenario better and provide you with accurate solution

http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

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

Go to Top of Page

ostinoh
Yak Posting Veteran

66 Posts

Posted - 2011-12-07 : 08:53:01
ok let me see if I can provide what is needed. Here is the script broken down with no filters added. The new column name is in bold with the Inner Join.

SELECT r.name as Resource_Name,rw.name as Business_Unit,t.name
FROM Resources r WITH(NOLOCK)
INNER JOIN Workgroupmember wgm WITH(NOLOCK) ON wgm.ResourceID = R.ResourceID and wgm.historical = 0
INNER JOIN Workgroup rw WITH(NOLOCK) ON rw.workgroupid = wgm.workgroupid
INNER JOIN Tasks t WITH(NOLOCK) ON t.taskid = ta.taskid
WHERE r.deleted = 0

When I run the above script I get the following error in SSMS.

Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "ta.taskid" could not be bound.


If the above script runs currently without the Task two columns are returned with users name and what department they work in. The new column Task would return if they are missing a PTO or Hoilday entry.

Please let me know what else you might need.

Thank you
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-07 : 08:57:57
quote:
Originally posted by ostinoh

ok let me see if I can provide what is needed. Here is the script broken down with no filters added. The new column name is in bold with the Inner Join.

SELECT r.name as Resource_Name,rw.name as Business_Unit,t.name
FROM Resources r WITH(NOLOCK)
INNER JOIN Workgroupmember wgm WITH(NOLOCK) ON wgm.ResourceID = R.ResourceID and wgm.historical = 0
INNER JOIN Workgroup rw WITH(NOLOCK) ON rw.workgroupid = wgm.workgroupid
INNER JOIN Tasks t WITH(NOLOCK) ON t.taskid = ta.taskid
WHERE r.deleted = 0

When I run the above script I get the following error in SSMS.

Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "ta.taskid" could not be bound.


If the above script runs currently without the Task two columns are returned with users name and what department they work in. The new column Task would return if they are missing a PTO or Hoilday entry.

Please let me know what else you might need.

Thank you



did you atleast read the posted link once
It clearly suggests how to provide sample data and ask a question

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

Go to Top of Page

ostinoh
Yak Posting Veteran

66 Posts

Posted - 2011-12-07 : 09:30:39
Let me try this again...

USE [Changepoint]
GO
/****** Object: Table [dbo].[Resources] Script Date: 12/07/2011 09:25:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Resources](
[ResourceId] [uniqueidentifier] NOT NULL,
[Name] [nvarchar](200) NOT NULL,
[FirstName] [nvarchar](50) NULL,
[MiddleName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[Description] [ntext] NULL,
[UserDefinedResourceId] [nvarchar](255) NULL,
[Title] [nvarchar](255) NULL,
[Language] [varchar](3) NULL,
[BaseCurrency] [varchar](3) NULL,
[WebPassword] [varchar](255) NULL,
[MailURL] [nvarchar](255) NULL,
[Signature] [varchar](50) NULL,
[CreatedOn] [datetime] NOT NULL,
[CreatedBy] [uniqueidentifier] NOT NULL,
[UpdatedOn] [datetime] NULL,
[UpdatedBy] [uniqueidentifier] NULL,
[Deleted] [bit] NOT NULL,
[LastLogin] [datetime] NULL,
[ResourceType] [varchar](3) NOT NULL CONSTRAINT [DF__Resource__Resour__1DFCE517] DEFAULT ('E'),
[Licensed] [bit] NOT NULL,
[ReportsTo] [uniqueidentifier] NULL,
[DisableEmail] [bit] NOT NULL CONSTRAINT [DF__Resource__Disabl__1FE52D89] DEFAULT (0),
[Location] [nvarchar](255) NULL,
[IndustryExperience] [nvarchar](255) NULL,
[EmployeeType] [varchar](3) NULL,
[License] [int] NOT NULL CONSTRAINT [DF__Resource__Licens__16F4B8DF] DEFAULT (0),
[AlternateName] [nvarchar](200) NULL,
[AllowGaps] [bit] NOT NULL CONSTRAINT [DF__Resource__AllowG__42C9197C] DEFAULT (1),
[LCID] [smallint] NOT NULL CONSTRAINT [DF__resource__LCID__4D46A7EF] DEFAULT (1033),
[FailedLoginAttempts] [tinyint] NULL,
[DisableLogin] [bit] NOT NULL CONSTRAINT [DF__Resource__Disabl__5C88EB7F] DEFAULT (0),
[PasswordSet] [datetime] NOT NULL CONSTRAINT [DF__Resource__Passwo__5D7D0FB8] DEFAULT (1 / 1 / 1900),
[PrimaryFunctionId] [uniqueidentifier] NOT NULL,
[HireDate] [datetime] NULL,
[TerminationDate] [datetime] NULL,
[UserMetaTags] [nvarchar](255) NOT NULL DEFAULT (''),
[SystemMetaTags] [nvarchar](255) NOT NULL DEFAULT (''),
[APILoginAttempts] [tinyint] NULL,
[CostCenterId] [uniqueidentifier] NOT NULL DEFAULT ('{00000000-0000-0000-0000-000000000000}'),
[CUAManaged] [bit] NOT NULL DEFAULT (0),
CONSTRAINT [PK__Resource__21A0F6C4] PRIMARY KEY CLUSTERED
(
[ResourceId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO
SET ANSI_PADDING OFF


INSERT INTO [Changepoint].[dbo].[Resources]
([ResourceId]
,[Name]
,[FirstName]
,[MiddleName]
,[LastName]
,[Description]
,[UserDefinedResourceId]
,[Title]
,[Language]
,[BaseCurrency]
,[WebPassword]
,[MailURL]
,[Signature]
,[CreatedOn]
,[CreatedBy]
,[UpdatedOn]
,[UpdatedBy]
,[Deleted]
,[LastLogin]
,[ResourceType]
,[Licensed]
,[ReportsTo]
,[DisableEmail]
,[Location]
,[IndustryExperience]
,[EmployeeType]
,[License]
,[AlternateName]
,[AllowGaps]
,[LCID]
,[FailedLoginAttempts]
,[DisableLogin]
,[PasswordSet]
,[PrimaryFunctionId]
,[HireDate]
,[TerminationDate]
,[UserMetaTags]
,[SystemMetaTags]
,[APILoginAttempts]
,[CostCenterId]
,[CUAManaged])
VALUES
(<ResourceId, uniqueidentifier,>
,<Name, nvarchar(200),>
,<FirstName, nvarchar(50),>
,<MiddleName, nvarchar(50),>
,<LastName, nvarchar(50),>
,<Description, ntext,>
,<UserDefinedResourceId, nvarchar(255),>
,<Title, nvarchar(255),>
,<Language, varchar(3),>
,<BaseCurrency, varchar(3),>
,<WebPassword, varchar(255),>
,<MailURL, nvarchar(255),>
,<Signature, varchar(50),>
,<CreatedOn, datetime,>
,<CreatedBy, uniqueidentifier,>
,<UpdatedOn, datetime,>
,<UpdatedBy, uniqueidentifier,>
,<Deleted, bit,>
,<LastLogin, datetime,>
,<ResourceType, varchar(3),>
,<Licensed, bit,>
,<ReportsTo, uniqueidentifier,>
,<DisableEmail, bit,>
,<Location, nvarchar(255),>
,<IndustryExperience, nvarchar(255),>
,<EmployeeType, varchar(3),>
,<License, int,>
,<AlternateName, nvarchar(200),>
,<AllowGaps, bit,>
,<LCID, smallint,>
,<FailedLoginAttempts, tinyint,>
,<DisableLogin, bit,>
,<PasswordSet, datetime,>
,<PrimaryFunctionId, uniqueidentifier,>
,<HireDate, datetime,>
,<TerminationDate, datetime,>
,<UserMetaTags, nvarchar(255),>
,<SystemMetaTags, nvarchar(255),>
,<APILoginAttempts, tinyint,>
,<CostCenterId, uniqueidentifier,>
,<CUAManaged, bit,>)


SELECT r.name AS Resource_Name, rw.name AS Business_Unit, t.NAME
FROM Resources r
INNER JOIN Workgroupmember wgm ON wgm.ResourceID = R.ResourceID and wgm.historical = 0
INNER JOIN Workgroup rw ON rw.workgroupid = wgm.workgroupid
INNER JOIN Tasks t ON t.taskid = ta.taskid
WHERE r.deleted = 0
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-07 : 09:33:19
you're using ta. and there's no table named ta in query which is why the error

SELECT r.name AS Resource_Name, rw.name AS Business_Unit, t.NAME
FROM Resources r
INNER JOIN Workgroupmember wgm ON wgm.ResourceID = R.ResourceID and wgm.historical = 0
INNER JOIN Workgroup rw ON rw.workgroupid = wgm.workgroupid
INNER JOIN Tasks t ON t.taskid = ta.taskid
WHERE r.deleted = 0


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

Go to Top of Page
   

- Advertisement -