2016-11-16 66 views
1

我有一個「匹配」的情況下,我需要匹配表中的記錄。匹配2波。第二個「波」是基於第一個「波」

我已經改變了我的使用羅斯文數據庫的情況..爲了說明的目的。

給定一組「數據」(放在下面的@holder表中),我需要根據以下標準找到匹配項。

如果這兩個姓氏和名字的比賽,比賽在兩個或多個以下:(城邦聚會或郵編),電話,分機

如果在任姓或姓的比賽,比賽中的一個三個或更多的以下內容:(城邦聚會或郵編),電話, 擴展

注意,「城邦在一起或ZIP」的意思,我需要匹配都市的組合和狀態........或zip ..........如果所有三個匹配,(城市狀態和zip),仍然應該只計爲「1」爲「(西umnCityStateZipEnum + ColumnHomePhoneEnum + ColumnExtensionEnum)「計算。

我想出了以下內容。但我有左連接。

是否有另一種方法在SQL中執行此類問題?

Use Northwind /* Or NorthwindPartial */ 
GO 

declare @holder table (holderidentitykey int identity (1,1), lastname varchar(32) , firstname varchar(48) , city varchar(32) , stateabbr varchar(32) , zip varchar(5) , homephone varchar(16) , extension varchar(8)) 

insert into @holder (lastname , firstname , city , stateabbr, zip, homephone , extension) 
select null , null, null, null, null , null, null 
union all select 'Davolio' , 'Nancy', null, null, '98122' , '(206) 555-9857', null /* should 'match'. lastname, firstname and TWO of the other data-elements */ 
union all select 'Davolio' , null, null, null, null , null, null 
union all select 'Fuller' , 'Andrew', 'Tacoma', 'WA', null , null, null 
union all select 'Peacock' , 'MaggyNotAMatchNoPhone', 'Redmond', 'WA', '98052' , null, null 
union all select 'Peacock' , 'MaggyNotAMatchWithPhoneAndExtension', 'Redmond', 'WA', '98052' , '(206) 555-8122', '5176' /* should 'match'. lastname and THREE of the other data-elements */ 

/* 

    If both lastname and firstname match, match on TWO or more of the following : (city-state-together OR zip) , phone, extension 

    If one of either lastname OR firstname match, match on THREE or more of the following : (city-state-together OR zip) , phone, extension 
*/ 


select distinct * from 
(
    select 
    holderidentitykey, 
    ColumnLastNameFirstNameEnum = 
     case 
      when h.lastname = eLastName.LastName and h.firstname = eFirstName.FirstName then 2 
      when h.lastname = eLastName.LastName then 1 
      when h.firstname = eFirstName.FirstName then 1 
      else 0 
     end 
, 
    ColumnCityStateZipEnum = 
     case 
      when h.zip = eZip.PostalCode then 1 
      when h.city = eCity.City and h.stateabbr = eState.Region then 1 
      else 0 
     end 
, 
    ColumnHomePhoneEnum = 
     case 
      when h.homephone = eHomePhone.HomePhone then 1 
      else 0 
     end 
, 
    ColumnExtensionEnum = 
     case 
      when h.extension = eExtension.Extension then 1 
      else 0 
     end 
    , eLastName.LastName , eFirstName.FirstName, eZip.PostalCode, eCity.City, eState.Region, eHomePhone.HomePhone, eExtension.Extension 
    from 
    @holder h 
    left join dbo.Employees eLastName on h.lastname = eLastName.LastName 
    left join dbo.Employees eFirstName on h.firstname = eFirstName.FirstName 
    left join dbo.Employees eZip on h.zip = eZip.PostalCode 
    left join dbo.Employees eCity on h.city = eCity.City 
    left join dbo.Employees eState on h.stateabbr = eState.Region 
    left join dbo.Employees eHomePhone on h.homephone = eHomePhone.HomePhone 
    left join dbo.Employees eExtension on h.extension = eExtension.Extension 
) as derived1 
where 
    derived1.ColumnLastNameFirstNameEnum >= 2 and (ColumnCityStateZipEnum + ColumnHomePhoneEnum + ColumnExtensionEnum) >= 2 
    OR 
    derived1.ColumnLastNameFirstNameEnum >= 1 and (ColumnCityStateZipEnum + ColumnHomePhoneEnum + ColumnExtensionEnum) >= 3 



-- select * from dbo.Employees e 

這是一個「部分」Northwind創造,如果你沒有一個方便。

SET NOCOUNT ON 
GO 

USE master 
GO 
if exists (select * from sysdatabases where name='NorthwindPartial') 
     drop database NorthwindPartial 
go 

DECLARE @device_directory NVARCHAR(520) 
SELECT @device_directory = SUBSTRING(filename, 1, CHARINDEX(N'master.mdf', LOWER(filename)) - 1) 
FROM master.dbo.sysaltfiles WHERE dbid = 1 AND fileid = 1 

EXECUTE (N'CREATE DATABASE NorthwindPartial 
    ON PRIMARY (NAME = N''NorthwindPartial'', FILENAME = N''' + @device_directory + N'northwndPartial.mdf'') 
    LOG ON (NAME = N''NorthwindPartial_log'', FILENAME = N''' + @device_directory + N'northwndPartial.ldf'')') 
go 


GO 

set quoted_identifier on 
GO 

/* Set DATEFORMAT so that the date strings are interpreted correctly regardless of 
    the default DATEFORMAT on the server. 
*/ 
SET DATEFORMAT mdy 
GO 
use "NorthwindPartial" 
go 

if exists (select * from sysobjects where id = object_id('dbo.Employees') and sysstat & 0xf = 3) 
    drop table "dbo"."Employees" 
GO 
CREATE TABLE "Employees" (
    "EmployeeID" "int" IDENTITY (1, 1) NOT NULL , 
    "LastName" nvarchar (20) NOT NULL , 
    "FirstName" nvarchar (10) NOT NULL , 
    "Title" nvarchar (30) NULL , 
    "TitleOfCourtesy" nvarchar (25) NULL , 
    "BirthDate" "datetime" NULL , 
    "HireDate" "datetime" NULL , 
    "Address" nvarchar (60) NULL , 
    "City" nvarchar (15) NULL , 
    "Region" nvarchar (15) NULL , 
    "PostalCode" nvarchar (10) NULL , 
    "Country" nvarchar (15) NULL , 
    "HomePhone" nvarchar (24) NULL , 
    "Extension" nvarchar (4) NULL , 
    "Photo" "image" NULL , 
    "Notes" "ntext" NULL , 
    "ReportsTo" "int" NULL , 
    "PhotoPath" nvarchar (255) NULL , 
    CONSTRAINT "PK_Employees" PRIMARY KEY CLUSTERED 
    (
     "EmployeeID" 
    ), 
    CONSTRAINT "FK_Employees_Employees" FOREIGN KEY 
    (
     "ReportsTo" 
    ) REFERENCES "dbo"."Employees" (
     "EmployeeID" 
    ), 
    CONSTRAINT "CK_Birthdate" CHECK (BirthDate < getdate()) 
) 
GO 
CREATE INDEX "LastName" ON "dbo"."Employees"("LastName") 
GO 
CREATE INDEX "PostalCode" ON "dbo"."Employees"("PostalCode") 
GO 


set quoted_identifier on 
go 
set identity_insert "Employees" on 
go 
ALTER TABLE "Employees" NOCHECK CONSTRAINT ALL 
go 
INSERT "Employees"("EmployeeID","LastName","FirstName","Title","TitleOfCourtesy","BirthDate","HireDate","Address","City","Region","PostalCode","Country","HomePhone","Extension","Photo","Notes","ReportsTo","PhotoPath") VALUES(1,'Davolio','Nancy','Sales Representative','Ms.','12/08/1948','05/01/1992','507 - 20th Ave. E. 
Apt. 2A','Seattle','WA','98122','USA','(206) 555-9857','5467',null,'Education includes a BA in psychology from Colorado State University in 1970. She also completed "The Art of the Cold Call." Nancy is a member of Toastmasters International.',2,'http://accweb/emmployees/davolio.bmp') 
GO 
INSERT "Employees"("EmployeeID","LastName","FirstName","Title","TitleOfCourtesy","BirthDate","HireDate","Address","City","Region","PostalCode","Country","HomePhone","Extension","Photo","Notes","ReportsTo","PhotoPath") VALUES(2,'Fuller','Andrew','Vice President, Sales','Dr.','02/19/1952','08/14/1992','908 W. Capital Way','Tacoma','WA','98401','USA','(206) 555-9482','3457',null,'Andrew received his BTS commercial in 1974 and a Ph.D. in international marketing from the University of Dallas in 1981. He is fluent in French and Italian and reads German. He joined the company as a sales representative, was promoted to sales manager in January 1992 and to vice president of sales in March 1993. Andrew is a member of the Sales Management Roundtable, the Seattle Chamber of Commerce, and the Pacific Rim Importers Association.',NULL,'http://accweb/emmployees/fuller.bmp') 
GO 
INSERT "Employees"("EmployeeID","LastName","FirstName","Title","TitleOfCourtesy","BirthDate","HireDate","Address","City","Region","PostalCode","Country","HomePhone","Extension","Photo","Notes","ReportsTo","PhotoPath") VALUES(3,'Leverling','Janet','Sales Representative','Ms.','08/30/1963','04/01/1992','722 Moss Bay Blvd.','Kirkland','WA','98033','USA','(206) 555-3412','3355',null,'Janet has a BS degree in chemistry from Boston College (1984). She has also completed a certificate program in food retailing management. Janet was hired as a sales associate in 1991 and promoted to sales representative in February 1992.',2,'http://accweb/emmployees/leverling.bmp') 
GO 
INSERT "Employees"("EmployeeID","LastName","FirstName","Title","TitleOfCourtesy","BirthDate","HireDate","Address","City","Region","PostalCode","Country","HomePhone","Extension","Photo","Notes","ReportsTo","PhotoPath") VALUES(4,'Peacock','Margaret','Sales Representative','Mrs.','09/19/1937','05/03/1993','4110 Old Redmond Rd.','Redmond','WA','98052','USA','(206) 555-8122','5176',null,'Margaret holds a BA in English literature from Concordia College (1958) and an MA from the American Institute of Culinary Arts (1966). She was assigned to the London office temporarily from July through November 1992.',2,'http://accweb/emmployees/peacock.bmp') 
GO 
INSERT "Employees"("EmployeeID","LastName","FirstName","Title","TitleOfCourtesy","BirthDate","HireDate","Address","City","Region","PostalCode","Country","HomePhone","Extension","Photo","Notes","ReportsTo","PhotoPath") VALUES(5,'Buchanan','Steven','Sales Manager','Mr.','03/04/1955','10/17/1993','14 Garrett Hill','London',NULL,'SW1 8JR','UK','(71) 555-4848','3453',null,'Steven Buchanan graduated from St. Andrews University, Scotland, with a BSC degree in 1976. Upon joining the company as a sales representative in 1992, he spent 6 months in an orientation program at the Seattle office and then returned to his permanent post in London. He was promoted to sales manager in March 1993. Mr. Buchanan has completed the courses "Successful Telemarketing" and "International Sales Management." He is fluent in French.',2,'http://accweb/emmployees/buchanan.bmp') 
GO 
INSERT "Employees"("EmployeeID","LastName","FirstName","Title","TitleOfCourtesy","BirthDate","HireDate","Address","City","Region","PostalCode","Country","HomePhone","Extension","Photo","Notes","ReportsTo","PhotoPath") VALUES(6,'Suyama','Michael','Sales Representative','Mr.','07/02/1963','10/17/1993','Coventry House 
Miner Rd.','London',NULL,'EC2 7JR','UK','(71) 555-7773','428',null,'Michael is a graduate of Sussex University (MA, economics, 1983) and the University of California at Los Angeles (MBA, marketing, 1986). He has also taken the courses "Multi-Cultural Selling" and "Time Management for the Sales Professional." He is fluent in Japanese and can read and write French, Portuguese, and Spanish.',5,'http://accweb/emmployees/davolio.bmp') 
GO 
INSERT "Employees"("EmployeeID","LastName","FirstName","Title","TitleOfCourtesy","BirthDate","HireDate","Address","City","Region","PostalCode","Country","HomePhone","Extension","Photo","Notes","ReportsTo","PhotoPath") VALUES(7,'King','Robert','Sales Representative','Mr.','05/29/1960','01/02/1994','Edgeham Hollow 
Winchester Way','London',NULL,'RG1 9SP','UK','(71) 555-5598','465',null,'Robert King served in the Peace Corps and traveled extensively before completing his degree in English at the University of Michigan in 1992, the year he joined the company. After completing a course entitled "Selling in Europe," he was transferred to the London office in March 1993.',5,'http://accweb/emmployees/davolio.bmp') 
GO 
INSERT "Employees"("EmployeeID","LastName","FirstName","Title","TitleOfCourtesy","BirthDate","HireDate","Address","City","Region","PostalCode","Country","HomePhone","Extension","Photo","Notes","ReportsTo","PhotoPath") VALUES(8,'Callahan','Laura','Inside Sales Coordinator','Ms.','01/09/1958','03/05/1994','4726 - 11th Ave. N.E.','Seattle','WA','98105','USA','(206) 555-1189','2344',null,'Laura received a BA in psychology from the University of Washington. She has also completed a course in business French. She reads and writes French.',2,'http://accweb/emmployees/davolio.bmp') 
GO 
INSERT "Employees"("EmployeeID","LastName","FirstName","Title","TitleOfCourtesy","BirthDate","HireDate","Address","City","Region","PostalCode","Country","HomePhone","Extension","Photo","Notes","ReportsTo","PhotoPath") VALUES(9,'Dodsworth','Anne','Sales Representative','Ms.','01/27/1966','11/15/1994','7 Houndstooth Rd.','London',NULL,'WG2 7LT','UK','(71) 555-4444','452',null,'Anne has a BA degree in English from St. Lawrence College. She is fluent in French and German.',5,'http://accweb/emmployees/davolio.bmp') 
go 
set identity_insert "Employees" off 
go 
ALTER TABLE "Employees" CHECK CONSTRAINT ALL 
go 
set quoted_identifier on 
go 


Select * from "Employees" 
+0

爲了幫助我理解,提供的代碼是否按照您的規則正確匹配? – mendosi

+0

是的,我用我的代碼得到正確的結果。我正在尋找「解決問題的替代方法」。 – granadaCoder

回答

1

這可能有助於分析您的匹配規則一點,如果我們打破它,我們可以看到,對於比賽的不可轉讓的條件是,要麼FirstNameLastName匹配。因此,讓我們創建一個查詢,我們從員工表連接只有那些行:

... 
FROM @holder As h 
JOIN Employee As e 
    ON h.FirstName = e.FirstName 
    OR h.LastName = e.LastName 
... 

現在我們只是在尋找符合最低標準的行,我們可以評估等。基本上,你的規則說,如果只將FirstNameLastName比賽,那麼我們就需要最少三個以下的(讓我們假設,我們匹配FirstName):

  • 比賽LastName
  • 比賽CityState,或PostalCode
  • 比賽HomePhone
  • 比賽Extension

您提出了不同的規則,具體取決於如果FirstNameLastName匹配,但只要您有這兩個中的一個,那麼碰巧這些規則在數學上等同於我所採用的視角。

因此,我們可以將我們可能的匹配行計算在內,並只計算其中有多少匹配屬性,並在沒有足夠的位置過濾出行。

Select h.holderidentitykey, e.* 
    From @holder As h 
    Join Employees As e 
    On h.FirstName = e.FirstName 
    Or h.lastname = e.LastName 
    Where iif(h.firstname = e.firstname, 1, 0) + 
     iif(h.lastname = e.LastName, 1, 0) + 
     iif((h.city = e.City AND h.stateabbr = e.Region) OR h.zip = e.PostalCode, 1, 0) + 
     iif(h.homephone = e.HomePhone, 1, 0) + 
     iif(h.extension = e.Extension, 1, 0) >= 4; 

請注意,這種方法可能不能很好地擴展,如果你有大的表(1M +),並希望經常匹配,但是如果/當這些情況發生,然後你可以看看重構。

+0

感謝您的替代想法。我會把它扔進測試周期。但我會有大桌子。 – granadaCoder

+0

@granadaCoder在這種情況下,我會嘗試使用'FirstName'上匹配的所有行的'UNION'和那些匹配'Lastname'的行。 – mendosi