1

在此处输入图像描述

我什至不知道如何命名这个主题。

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

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

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

4 回答 4

0

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

但是使用 sql 返回一组多重排列的一种简单方法是创建一个返回笛卡尔积的查询。

例如 select a,b,c from (select 1 a union select 2 a union select 3 a) (select 10 b union select 20 b union select 30 b) (select 100 c union select 200 union select 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 将是一种排列。Tin 和 Tin2 将是另一个您将获得每个 NPI 的笛卡尔积。

于 2013-03-20T00:28:47.653 回答
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
于 2013-03-20T00:31:00.523 回答
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.

还是我误解了这个问题?

于 2013-03-20T00:41:30.163 回答
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%')
于 2013-03-21T17:41:54.157 回答