Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
First- Last- ID Exit_Date
Jan Klaas 5656 1-1-1900
Jan Klaas 5656 1-1-1900
Jan Klaas 5656 25-12-2017
Peter Jobs 1223 1-1-1900
Mark Mulder 4456 24-6-2017
Taylor Mulder 1235 30-11-2017
Big Al 4567 1-1-2017
Big Al 4567 30-10-2017
big Al 4567 5-1-2017
Mike Mulder 1232 30-8-2017

I want to have a table with only the people that are leaving in the future and only the one's that don't have a other active entry.

the problem is that a user can have more then one entry. 1-1-1900 is used in the database for infinity. User Big Al has one exit date in the future. I want the last date from him 30-10-2017. Jan Klaas has two infinity dates I don't want him.
Mark Mulder has a date in the past.

First- Last- ID Exit_Date
Taylor Mulder 1235 30-11-2017
Big Al 4567 30-10-2017
Mike Mulder 1232 30-8-2017

What I have tried:

have tried some select and group query's but i'm no SQL master if some one can please help or point met to the correct direction.
Posted
Updated 29-Jul-17 12:37pm

1 solution

I may not be understanding but it sounds like, if any person has a date of 1/1/1900 you don't want them, which should eliminate Jan Klass correct?

For the sake of this example, I'm ignoring the ID column since it appears to not matter in the example you've put forth.

SQL
DECLARE @ExitTable TABLE (
	FirstName varchar(200) NULL,
	LastName VARCHAR(25) NULL,
	Exit_Date DATETIME NULL
);

INSERT INTO @ExitTable( FirstName, LastName, Exit_Date )
VALUES  ( 'Jan', -- FirstName - varchar(200)
          'Klass', -- LastName - varchar(25)
          '1-1-1900'-- Exit_Date - datetime
          )
INSERT INTO @ExitTable( FirstName, LastName, Exit_Date )
VALUES  ( 'Jan', -- FirstName - varchar(200)
          'Klass', -- LastName - varchar(25)
          '1-1-1900'-- Exit_Date - datetime
          )
INSERT INTO @ExitTable( FirstName, LastName, Exit_Date )
VALUES  ( 'Jan', -- FirstName - varchar(200)
          'Klass', -- LastName - varchar(25)
          '12-25-2017'-- Exit_Date - datetime
          )
INSERT INTO @ExitTable( FirstName, LastName, Exit_Date )
VALUES  ( 'Peter', -- FirstName - varchar(200)
          'Jobs', -- LastName - varchar(25)
          '1-1-1900'-- Exit_Date - datetime
          )
INSERT INTO @ExitTable( FirstName, LastName, Exit_Date )
VALUES  ( 'Mark', -- FirstName - varchar(200)
          'Mulder', -- LastName - varchar(25)
          '6/24/2017'-- Exit_Date - datetime
          )
INSERT INTO @ExitTable( FirstName, LastName, Exit_Date )
VALUES  ( 'Taylor', -- FirstName - varchar(200)
          'Mulder', -- LastName - varchar(25)
          '11/30/2017'-- Exit_Date - datetime
          )
INSERT INTO @ExitTable( FirstName, LastName, Exit_Date )
VALUES  ( 'Mike', -- FirstName - varchar(200)
          'Mulder', -- LastName - varchar(25)
          '8/30/2017'-- Exit_Date - datetime
          )
INSERT INTO @ExitTable( FirstName, LastName, Exit_Date )
VALUES  ( 'Big', -- FirstName - varchar(200)
          'AL', -- LastName - varchar(25)
          '1-1-2017'-- Exit_Date - datetime
          )
INSERT INTO @ExitTable( FirstName, LastName, Exit_Date )
VALUES  ( 'Big', -- FirstName - varchar(200)
          'AL', -- LastName - varchar(25)
          '10/30/2017'-- Exit_Date - datetime
          )
INSERT INTO @ExitTable( FirstName, LastName, Exit_Date )
VALUES  ( 'Big', -- FirstName - varchar(200)
          'AL', -- LastName - varchar(25)
          '1/5/2017'-- Exit_Date - datetime
          )


SELECT * FROM @ExitTable AS A WHERE 
CAST(A.Exit_Date AS DATE) > CAST('7/29/2017' AS DATE) --7/29 can be swapped out for GETDATE()
AND NOT EXISTS (SELECT * FROM @ExitTable AS B WHERE CAST(B.Exit_Date AS DATE) = CAST('1/1/1900' AS DATE) AND B.FirstName = A.FirstName AND B.LastName = A.LastName)


The NOT EXISTS eliminates any record where they may have an entry of 1/1/1900

Expected output should be

Taylor	Mulder	2017-11-30 00:00:00.000
Mike	Mulder	2017-08-30 00:00:00.000
Big	AL	2017-10-30 00:00:00.000
 
Share this answer
 
Comments
Member 10376393 30-Jul-17 15:35pm    
Thanks David for your quick response add the moment I don't have acces to the computer at my work to try it. I wil do tomorrow Thanks for your help any way!!

I don't know if I can ignore the ID column the datebase has around a 1000 entry's I don't know what the change would be if there is more than one "Jan Klaas" with a different ID's representing a different person.
David_Wimbley 30-Jul-17 18:22pm    
I only ignored the ID cause i was lazy. You can swap out the B.FirstName = A.FirstName and B.LastName = A.LastName with A.Id = B.Id and that should, in theory, take care of a situation where there are 2 unique jan klaas's with different ID's.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900