Author |
Topic |
DazlerD
Starting Member
8 Posts |
Posted - 2012-01-16 : 12:19:50
|
Hi allIm trying to link two tables but they dont have a direct link. I have to link them on surname, address1-5, postcode using a built in sql function. The contact table already has a matchcode but the Landing page doesnt.Here's what i've got:select c.Title, c.Forename,c.Surname, c.Address1, c.Address2, c.Address3, c.Address4, c.Address5, c.Town, c.Postcode, l.DateOfBirth, l.Telephone, l.TicketsRequested, l.DateReceived, l.Emailfrom Contacts cinner join (select top 1 * from LandingPage) l on CreateMatchCode(l.Surname, l.PostCode, l.Address1, l.Address2, l.Address3, l.Address4, l.Address5, '') = c.MatchCodewhere c.Title not like 'Mrs%' and c.Surname <> 'Anon' and l.Transferred = 1 and l.AgeVerified = 0 It ss bringing back 0 records.I know there are some records in both tables that match.Be grateful for some suggestions please.Thanks |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-16 : 12:28:26
|
is creatematchcode a user defined function? if its so, the performance of join would be really a concern for large datasets------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
DazlerD
Starting Member
8 Posts |
Posted - 2012-01-16 : 12:31:04
|
Hi Yeah CreateMatchCode uses the parameters to create a code for the contact |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-16 : 12:33:41
|
may be this?select c.Title, c.Forename,c.Surname, c.Address1, c.Address2, c.Address3, c.Address4, c.Address5, c.Town, c.Postcode, l.DateOfBirth, l.Telephone, l.TicketsRequested, l.DateReceived, l.Emailfrom Contacts cinner join (select top 1 *,dbo.CreateMatchCode(l.Surname, l.PostCode, l.Address1, l.Address2, l.Address3, l.Address4, l.Address5, '') as MatchCode from LandingPage) l on l.MatchCode = c.MatchCodewhere c.Title not like 'Mrs%' and c.Surname <> 'Anon' and l.Transferred = 1 and l.AgeVerified = 0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
DazlerD
Starting Member
8 Posts |
Posted - 2012-01-17 : 05:30:07
|
Sorted it thanksFirstly I set up a temp table so I could populate a field which is the calculated MatchCode. Set up an index on this.select ID, CreateMatchCode(Surname, PostCode, Address1, Address2, Address3, Address4, Address5, '') as MatchCode into dbo.#tmp_LandingPage from LandingPageCREATE NONCLUSTERED INDEX IX_Control ON dbo.#tmp_LandingPage( MatchCode) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] Then run the code:select c.SupporterNo, c.Title, c.Forename,c.Surname, c.Address1, c.Address2, c.Address3, c.Address4, c.Address5, c.Town, c.Postcode,l.DateOfBirth, l.Telephone, l.TicketsRequested, l.DateReceived, l.Emailfrom Contacts c inner join (select MAX(ID) as ID, MatchCode from #tmp_LandingPage group by MatchCode) RecentLanding on RecentLanding.MatchCode = c.MatchCode inner join dbo.LandingPage l on l.ID = RecentLanding.IDwhere c.Title not like 'Mrs%' and c.Surname <> 'Anon' and l.Transferred = 1 and l.AgeVerified = 0 and l.BrightsourceMailingFileID = 0group by c.SupporterNo, c.Title, c.Forename,c.Surname, c.Address1, c.Address2, c.Address3, c.Address4, c.Address5, c.Town, c.Postcode, l.DateOfBirth, l.Telephone, l.TicketsRequested, l.DateReceived, l.Email |
 |
|
|
|
|