Создание множественных записей, если значение Column1 таблицы A отсутствует в столбце Coll1 Значение таблицы B и т. Д. - PullRequest
0 голосов
/ 28 февраля 2020

У меня есть две таблицы Таблица A и Таблица B, которые имеют иерархические данные, если сравнивать на уровне столбца1 две таблицы, если Столбец1 Таблицы A Не выходит в столбец1 Таблицы B, затем создать запись только со значением Столбец1 Таблицы A, Когда По сравнению с двумя таблицами уровня Столбец1 + Столбец2, если Столбец1 + столбец2 Таблицы А не существует в столбце Столбец + Столбец2 таблицы В, затем создайте две записи, первые записи должны иметь только значения столбца1 из таблицы А, а вторая запись должна иметь значения столбцов1 и столбца2. и так далее. Любые идеи о том, как добиться этого с помощью SQL или SSIS?

Таблица A

Division    State      County    City
-----------------------------------------
North       OH         ABCDE     CLE
South       TX         Dallas    Irving
East        NC         EFGH      Charlotte

Таблица B

Division   State      County     City
-----------------------------------------
North       OH         ABCDE      Akron
East        NC         PQRST      Duluth
South       LA         GHJKL      Boca

Ожидаемый результат:

Division   State     County  City
------------------------------------------------------------------------------------
North      Null       Null    Null -- City(L4) got changed created this record at L1
North      OH         Null    Null -- City(L4) got changed created this record at L2 
North      OH         ABCDE   Null -- City(L4) got changed created this record at L3
East       Null       Null    Null -- City(L4) got changed created this record at L1
East       NC         Null    Null -- City(L4) got changed created this record at L2 
East       NC         EFGH    Null -- City(L4) got changed created this record at L3
East       Null       Null    Null -- County(L3) changed created L1 record
East       NC         Null    Null -- County(L3) changed Created L2 record
South      Null       Null    Null -- State (L2) Changed Created L1 record

1 Ответ

0 голосов
/ 28 февраля 2020

Я считаю, что это то, что вы хотели бы после ...

/* Setup, using table variables for this example */
declare @TableA table (Division varchar(50), [State] varchar(50), County varchar(50), City varchar(50))
declare @TableB table (Division varchar(50), [State] varchar(50), County varchar(50), City varchar(50))

insert into @TableA values   ('North','OH','ABCDE','CLE')
                            ,('South','TX','Dallas','Irving')
                            ,('East','NC','EFGH','Charlotte')

insert into @TableB values   ('North','OH','ABCDE','Akron')
                            ,('East','NC','PQRST','Duluth')
                            ,('South','LA','GHJKL','Boca')

--review initital tables
select * from @TableA
select * from @TableB

-- Top Level of Hierarchy...
insert into @TableB
select distinct
     A.Division
    ,NULL as [State]
    ,NULL as [County]
    ,NULL as [City]
from
    @TableA A
    left join
    @TableB B on A.Division = B.Division
             and B.[State] is null
             and B.County is null
             and B.City is null
where
    B.Division is null

-- 2nd Level of Hierarchy...
insert into @TableB
select distinct
     A.Division
    ,A.[State] as [State]
    ,NULL as [County]
    ,NULL as [City]
from
    @TableA A
    left join
    @TableB B on A.Division = B.Division
             and A.[State] = B.[State]
             and B.County is null
             and B.City is null
where
    B.Division is null
    and B.[State] is null

-- 3rd Level of Hierarchy...
insert into @TableB
select distinct
     A.Division
    ,A.[State] as [State]
    ,A.County as [County]
    ,NULL as [City]
from
    @TableA A
    left join
    @TableB B on A.Division = B.Division
             and A.[State] = B.[State]
             and A.County = B.County
             and B.City is null
where
    B.Division is null
    and B.[State] is null
    and B.County is null


-- Review results
select
    *
from
    @TableB
order by
    Division
    ,[State]
    ,County
    ,City

Результаты :

Division    State   County  City
---------------------------------------------
East        NULL    NULL    NULL
East        NC      NULL    NULL
East        NC      EFGH    NULL
East        NC      PQRST   Duluth
North       NULL    NULL    NULL
North       OH      NULL    NULL
North       OH      ABCDE   NULL
North       OH      ABCDE   Akron
South       NULL    NULL    NULL
South       LA      GHJKL   Boca
South       TX      NULL    NULL
South       TX      Dallas  NULL
...