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 |
borntorun
Starting Member
1 Post |
Posted - 2010-11-24 : 04:51:33
|
Hi there,I'm wondering if someone on here could help me with some guidance on how I would go about doing the following based on this sample table data:TableAUniqueID Surname MothersBirthSurname DateOfBirth1 Bloggs Jones 03/03/19902 Soap Smith 25/12/19703 Smith Collins 30/06/19754 Jones Fitzgerald 04/07/19765 Bloggs Jones 04/03/19906 Smith Collins 28/06/1975What I am trying to achieve is select all records where the Surname and MothersBirthSurname are the same and where the DateOfBirth is within -2 or +2 days of each other.I've been racking my brain on this for a while now and haven't been able to make much progress so am hoping that someone on here can at least start to send me in the right direction.Any help given will be much appreciated... |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-11-24 : 05:23:47
|
[code]-- creating a temp. table for testdatacreate table #tableA(UniqueID int,Surname varchar(20),MothersBirthSurname varchar(20),DateOfBirth datetime)-- insert testdatainsert #tableAselect 1,'Bloggs','Jones','19900303' union allselect 2, 'Soap', 'Smith', '19701225' union allselect 3, 'Smith', 'Collins', '19750630' union allselect 4, 'Jones', 'Fitzgerald', '19760704' union allselect 5, 'Bloggs', 'Jones', '19900304' union allselect 6, 'Smith', 'Collins', '19750628'-- solution (just replace #tableA by your tablename)select a.* from #tableA as ajoin #tableA as bon a.Surname = b.Surnameand a.MothersBirthSurname = b.MothersBirthSurnameand a.UniqueID <> b.UniqueIDand datediff(day,a.DateOfBirth,b.DateOfBirth) between -2 and 2order by a.Surname-- cleaning testdatadrop table #tableA[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-11-24 : 05:26:57
|
Next time please do your own job when asking a question and post the create and insert statement like above so we can copy and paste that to work out the solution. And please post your wanted output in relation to the testdata.thx No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
|
|
|
|
|