排列7,一次替换一个字段

时间:2013-03-20 00:14:31

标签: sql permutation

enter image description here

我甚至不知道如何命名这个话题。

这就是我想要做的。我有一组数据需要处理,它们可能有相同字段的几个版本。

例如,我有一个包含以下列的数据集:

FacilityName
FacilityAlias
TIN
TIN2
NPI
NPI2
NPI3
MailingAddress
MailingCity, State (they're given to me as 2 different fields but for my needs they're counted as 1)
MailingZIP
BillingAddress
Billing City, State
BillingZIP

我的最终数据集只有以下列

FacilityName
TIN
NPI
Address
CityState
ZIP

但我必须确保每个可能的字段组合都有一行, 所以我必须创建以下查询:

INSERT INTO MATCH ( FacilityName, TIN, NPI, Address, CityState, ZIP) SELECT FacilityName, TIN, NPI. BillingAddress, BillingCityState, BillingZIP FROM MATCHPREP;

INSERT INTO MATCH ( FacilityName, TIN, NPI, Address, CityState, ZIP) SELECT FacilityAlias, TIN, NPI. BillingAddress, BillingCityState, BillingZIP FROM MATCHPREP;

INSERT INTO MATCH ( FacilityName, TIN, NPI, Address, CityState, ZIP) SELECT FacilityName, TIN, NPI. MailingAddress, MailingCityState, MailingZIP FROM MATCHPREP;

INSERT INTO MATCH ( FacilityName, TIN, NPI, Address, CityState, ZIP) SELECT FacilityName, TIN2, NPI. BillingAddress, BillingCityState, BillingZIP FROM MATCHPREP;

INSERT INTO MATCH ( FacilityName, TIN, NPI, Address, CityState, ZIP) SELECT FacilityAlias, TIN2, NPI. BillingAddress, BillingCityState, BillingZIP FROM MATCHPREP;

INSERT INTO MATCH ( FacilityName, TIN, NPI, Address, CityState, ZIP) SELECT FacilityName, TIN2, NPI. MailingAddress, MailingCityState, MailingZIP FROM MATCHPREP;

等等。

这是怎么做到的?

4 个答案:

答案 0 :(得分:0)

要给出确切的答案是非常困难的,因为我不能100%确定你为什么要这样做。

但是使用sql返回一组多重排列的简单方法是创建一个返回cartisian产品的查询。

e.g。     从中选择a,b,c     (选择1一个联合选择2一个联合选择3 a)     (选择10 b联合选择20 b联合选择30 b)     (选择100 c union选择200 union选择300 c)

注意没有加入。子查询可以是来自matchprep表的不同查询。 在这个例子中,结果看起来像

1,10,100
1,10,300
1,10,300
...
...
3,30,100
3,30,200
3,30,300 

然后您可以插入返回数据集。

http://en.wikipedia.org/wiki/Cartesian_product

编辑:

必须如此:)

select  
name, tin, npi, address, state, zip
from (select distinct name from (select FacilityName as name from matchprep 
    union select FacilityAlias as name from matchprep) f cross join
(select distinct tin (select TIN as tin from matchprep 
    union select TIN2 as tin from matchprep)) t cross join
 (select distinct NPI from matchprep) n cross join
 (select distinct address,state,zip from (select BillingAddress as address, BillingCityState as state, BillingZIP as zip from matchprep 
    union select MailingAddress as address, MailingCityState as state, MailingZIP as zip from matchprep)) a

根据您的示例插入,邮寄地址详细信息和结算地址详细信息始终彼此不同。

FacilityName和Alias将是一个排列。 锡和Tin2将是另一个 你会得到每个NPI的笛卡尔积。

答案 1 :(得分:0)

如果我理解正确,您需要7个字段的所有可能组合。为此,请使用cross join

select  FacilityName, TIN, NPI, Address, CityState, ZIP
from (select distinct FacilityName from matchprep) f cross join
     (select distinct TIN from matchprep) t cross join
     (select distinct NPI from matchprep) n cross join
     (select distinct Address from matchprep) a cross join
     (select distinct CityState from matchprep) c cross join
     (select distinct ZIP from matchprep) z

答案 2 :(得分:0)

我假设你在问题的最后用示例插入覆盖了所有的排列,并且你不需要包括NPI2和NPI3作为NPI的排列。在这种情况下,您编写的几个插入应该可以工作,但使用UNION将它们组合成单个插入语句会更简单。

INSERT INTO MATCH ( FacilityName, TIN, NPI, Address, CityState, ZIP)
SELECT FacilityName, TIN, NPI. BillingAddress, BillingCityState, BillingZIP FROM MATCHPREP;
UNION
SELECT FacilityAlias, TIN, NPI. BillingAddress, BillingCityState, BillingZIP FROM MATCHPREP;
UNION 
SELECT FacilityName, TIN, NPI. MailingAddress, MailingCityState, MailingZIP FROM MATCHPREP;
etc.

或者我误解了这个问题?

答案 3 :(得分:0)

仅供参考,下面是我能够使用的版本

     SELECT A0.*
    ,a1.*
    ,a2.*
    ,a8.*
FROM (SELECT 'FacilityName' C1 FROM DUAL
      UNION
      SELECT 'TIN' FROM DUAL
      UNION
      SELECT 'NPI' FROM DUAL
      UNION
      SELECT 'Address' FROM DUAL) a0
    ,(SELECT 'FacilityName1' C2 FROM DUAL
      UNION
      SELECT 'TIN1' FROM DUAL
      UNION
      SELECT 'NPI1' FROM DUAL
      UNION
      SELECT 'Address1' FROM DUAL) a1
    ,(SELECT 'FacilityName2' C3 FROM DUAL
      UNION
      SELECT 'TIN2' FROM DUAL
      UNION
      SELECT 'NPI2' FROM DUAL
      UNION
      SELECT 'Address2' FROM DUAL) a2
    ,(SELECT 'FacilityName3' C4 FROM DUAL
      UNION
      SELECT 'TIN3' FROM DUAL
      UNION
      SELECT 'NPI3' FROM DUAL
      UNION
      SELECT 'Address3' FROM DUAL) a8
  WHERE   (   c1 LIKE 'FacilityName%'
       OR c2 LIKE 'FacilityName%'
       OR c3 LIKE 'FacilityName%'
       OR c4 LIKE 'FacilityName%')
     AND (   c1 LIKE 'TIN%'
       OR c2 LIKE 'TIN%'
       OR c3 LIKE 'TIN%'
       OR c4 LIKE 'TIN%')
     AND (   c1 LIKE 'NPI%'
       OR c2 LIKE 'NPI%'
       OR c3 LIKE 'NPI%'
       OR c4 LIKE 'NPI%')
     AND (   c1 LIKE 'Address%'
       OR c2 LIKE 'Address%'
       OR c3 LIKE 'Address%'
       OR c4 LIKE 'Address%')