|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Finding the right recordI have two tables Employee EmpID varchar(10) (primary key, unique values) EmpSSN varchar(9) And TimeSheet EmpID varchar(10) (foreign key to employee EmpID) TimeIn datetime The problem is that we have multiple Employees with the same EmpSSN. What I would like to do is find the EmpID's of these duplicates, and change the SSNs of all those dups to '000000000' except the one EmpID with the most recent TimeIn. Not every EmpID has a TimeSheet record. So, if I have EmpID = 1234 with SSN 123456789 And EmpID = 1235 with SSN 123456789 And time sheet records of EmpID = 1234 with TimeIn of '11/1/2005' And EmpID = 1235 with TimeIn of '10/1/2005' I'd like to change the EmpSSN of the Employee record, EmpID = 1235 to '000000000' Thanks for any help. -John John Baima wrote:
> The problem is that we have multiple Employees with the same EmpSSN. I think this will do:> What I would like to do is find the EmpID's of these duplicates, and > change the SSNs of all those dups to '000000000' except the one EmpID > with the most recent TimeIn. > > Not every EmpID has a TimeSheet record. update Employee set EmpSSN = '000000000' from Employee EM where EM.EmpID <> IsNull((select max(TimeIn) from Timesheet where Timesheet.EMID = EM.EmpID), EM.EmpID) The IsNull is to make sure the ones without timesheet records aren't deleted. -- HTH, Stijn Verrept.
Show quote
"Stijn Verrept" <TURN_moc.tfosyrtne@njits_AROUND> wrote: Thanks! For some reason (besides the insignificant typo), when I run>John Baima wrote: > >> The problem is that we have multiple Employees with the same EmpSSN. >> What I would like to do is find the EmpID's of these duplicates, and >> change the SSNs of all those dups to '000000000' except the one EmpID >> with the most recent TimeIn. >> >> Not every EmpID has a TimeSheet record. > >I think this will do: > >update Employee set EmpSSN = '000000000' from Employee EM where EM.EmpID ><> IsNull((select max(TimeIn) from Timesheet where Timesheet.EMID = >EM.EmpID), EM.EmpID) > >The IsNull is to make sure the ones without timesheet records aren't >deleted. this I get: The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value. -John John Baima wrote:
> Thanks! For some reason (besides the insignificant typo), when I run Damn made a mistake, this won't work in a million year :) I'll rewrite> this I get: > > The conversion of char data type to smalldatetime data type resulted > in an out-of-range smalldatetime value. it. -- HTH, Stijn Verrept. This will be better I think. Please supply DDL next time so we can
immediatly test if it works. update Employee set EmpSSN = '000000000' from Employee EM where EM.EmpID <> IsNull((select Top 1 Timesheet.EmpID from Timesheet where Timesheet.EMID = EM.EmpID order by TimeIn desc), EM.EmpID) -- HTH, Stijn Verrept. "Stijn Verrept" <TURN_moc.tfosyrtne@njits_AROUND> wrote: Thanks, but that does not work. When I run it with a select instead of>This will be better I think. Please supply DDL next time so we can >immediatly test if it works. > >update Employee set EmpSSN = '000000000' from Employee EM >where EM.EmpID <> IsNull((select Top 1 Timesheet.EmpID from Timesheet >where Timesheet.EMID = EM.EmpID order by TimeIn desc), EM.EmpID) the update, I do not get any rows. Here is the cut down DDL: CREATE TABLE [Employee] ( [EmpID] [varchar] (9) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [EmpSSN] [varchar] (9) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED ( [EmpID] ) WITH FILLFACTOR = 90 ON [PRIMARY] ) ON [PRIMARY] GO CREATE TABLE [TimeSheet] ( [EmpID] [varchar] (9) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [TimeIn] [smalldatetime] NOT NULL CONSTRAINT [DF_TimeSheet_TimeIn] DEFAULT (''), ) ON [PRIMARY] GO -John John Baima wrote:
> Thanks, but that does not work. When I run it with a select instead of update Employee set EmpSSN = '000000000' from Employee EM > the update, I do not get any rows. where EM.EmpID <> IsNull((select Top 1 Timesheet.EmpID from Timesheet inner join Employee EM2 on EM2.EmpID = Timesheet.EmpID where EM2.EmpSSN = EM.EmpSSN order by TimeIn desc), EM.EmpID) Ok this should be better. If you post please also post some testing data: www.aspfaq.com/5006 I hope this one works cause I'm off for a 4 week holiday to Vietnam tomorrow ;) -- HTH, Stijn Verrept. John
CREATE TABLE #Empl ( idNo INT NOT NULL IDENTITY(1,1) PRIMARY KEY, SSN VARCHAR(1000), EmplId INT, Hire_Date DATETIME ) INSERT INTO #Empl (SSN,EmplId,Hire_Date) VALUES ('5555555',20,'20050101') INSERT INTO #Empl (SSN,EmplId,Hire_Date)VALUES ('5555555',10,'20050102') INSERT INTO #Empl (SSN,EmplId,Hire_Date)VALUES ('YYYMM',10,'20050106') INSERT INTO #Empl (SSN,EmplId,Hire_Date) VALUES ('5555555',20,'20050104') UPDATE #Empl SET SSN='0000000' WHERE idNo IN ( SELECT B.idNo FROM #Empl A JOIN #Empl B ON A.idNo <> B.idNo AND A.SSN = B.SSN AND A.EmplId = B.EmplId) SELECT * FROM #Empl Show quote "John Baima" <john@nospam.com> wrote in message news:42sjq1t7ft508bfpidssl4uvcpte43v716@4ax.com... > My brain is not working right now and I need some help. > > I have two tables > Employee > EmpID varchar(10) (primary key, unique values) > EmpSSN varchar(9) > > And > TimeSheet > EmpID varchar(10) (foreign key to employee EmpID) > TimeIn datetime > > The problem is that we have multiple Employees with the same EmpSSN. > What I would like to do is find the EmpID's of these duplicates, and > change the SSNs of all those dups to '000000000' except the one EmpID > with the most recent TimeIn. > > Not every EmpID has a TimeSheet record. > > So, if I have > EmpID = 1234 with SSN 123456789 > And > EmpID = 1235 with SSN 123456789 > > And time sheet records of > EmpID = 1234 with TimeIn of '11/1/2005' > And > EmpID = 1235 with TimeIn of '10/1/2005' > > I'd like to change the EmpSSN of the Employee record, EmpID = 1235 to > '000000000' > > Thanks for any help. > > -John > Correction
UPDATE #Empl SET SSN='0000000' WHERE idNo IN ( SELECT B.idNo FROM #Empl A JOIN #Empl B ON A.idNo < B.idNo AND A.SSN = B.SSN AND A.EmplId = B.EmplId) Show quote "Uri Dimant" <u***@iscar.co.il> wrote in message news:OjqqZurBGHA.3936@TK2MSFTNGP12.phx.gbl... > John > CREATE TABLE #Empl > ( > idNo INT NOT NULL IDENTITY(1,1) PRIMARY KEY, > SSN VARCHAR(1000), > EmplId INT, > Hire_Date DATETIME > ) > > INSERT INTO #Empl (SSN,EmplId,Hire_Date) VALUES ('5555555',20,'20050101') > INSERT INTO #Empl (SSN,EmplId,Hire_Date)VALUES ('5555555',10,'20050102') > INSERT INTO #Empl (SSN,EmplId,Hire_Date)VALUES ('YYYMM',10,'20050106') > INSERT INTO #Empl (SSN,EmplId,Hire_Date) VALUES ('5555555',20,'20050104') > > UPDATE #Empl SET SSN='0000000' > WHERE idNo IN ( SELECT B.idNo > FROM #Empl A JOIN #Empl B > ON A.idNo <> B.idNo > AND A.SSN = B.SSN > AND A.EmplId = B.EmplId) > > > SELECT * FROM #Empl > > > "John Baima" <john@nospam.com> wrote in message > news:42sjq1t7ft508bfpidssl4uvcpte43v716@4ax.com... >> My brain is not working right now and I need some help. >> >> I have two tables >> Employee >> EmpID varchar(10) (primary key, unique values) >> EmpSSN varchar(9) >> >> And >> TimeSheet >> EmpID varchar(10) (foreign key to employee EmpID) >> TimeIn datetime >> >> The problem is that we have multiple Employees with the same EmpSSN. >> What I would like to do is find the EmpID's of these duplicates, and >> change the SSNs of all those dups to '000000000' except the one EmpID >> with the most recent TimeIn. >> >> Not every EmpID has a TimeSheet record. >> >> So, if I have >> EmpID = 1234 with SSN 123456789 >> And >> EmpID = 1235 with SSN 123456789 >> >> And time sheet records of >> EmpID = 1234 with TimeIn of '11/1/2005' >> And >> EmpID = 1235 with TimeIn of '10/1/2005' >> >> I'd like to change the EmpSSN of the Employee record, EmpID = 1235 to >> '000000000' >> >> Thanks for any help. >> >> -John >> > > Uri Dimant wrote:
> Correction Uri,> UPDATE #Empl SET SSN='0000000' > WHERE idNo IN ( SELECT B.idNo > FROM #Empl A JOIN #Empl B > ON A.idNo < B.idNo > AND A.SSN = B.SSN > AND A.EmplId = B.EmplId) He has 2 tables, Employee and Timesheet and needs to select it on the most recent TimeIn column where the SSN columns are identical. The EmpID don't need to be identical. -- Kind regards, Stijn Verrept. Hi
I did not follow his tables structure. I did mean to give him an idea how it could be done , that's all Show quote "Stijn Verrept" <TURN_moc.tfosyrtne@njits_AROUND> wrote in message news:idCdnden8vR06zfeRVny1g@scarlet.biz... > Uri Dimant wrote: > >> Correction >> UPDATE #Empl SET SSN='0000000' >> WHERE idNo IN ( SELECT B.idNo >> FROM #Empl A JOIN #Empl B >> ON A.idNo < B.idNo >> AND A.SSN = B.SSN >> AND A.EmplId = B.EmplId) > > Uri, > > He has 2 tables, Employee and Timesheet and needs to select it on the > most recent TimeIn column where the SSN columns are identical. The > EmpID don't need to be identical. > > -- > Kind regards, > > Stijn Verrept. |
|||||||||||||||||||||||