Я использую этот оператор merge
, но он не обновляет мою целевую таблицу. Он не выдает никаких ошибок и возвращает результаты cte, greater
. Я ожидал бы, что это обновит строки, которые должны быть обновлены, и вставит строки, которые должны быть вставлены, и вернет, сколько строк было затронуто.
with greater (provider) as (
select
provider
from
(
select
h.provider,
npi
from
hha h
join Quality q on h.provider = q.provider
group by
h.provider,
npi
) a
group by
provider
having
count(*) > 1
),
zip_npi (providernbr, zip, npi) as (
select
providernbr,
zip,
npi
from
npi
group by
providernbr,
zip,
npi
),
zip_quality (provider, zip, rating) as (
select
provider,
zip,
rating
from
Quality
)
select
a.provider,
c.npi,
'HHA',
max(rating) as rating
from
greater a
join zip_quality q on a.provider = q.provider
join Zip_npi c on q.provider = c.providernbr
and q.zip = c.zip
where
rating <> ''
group by
a.provider,
c.npi merge provider as [Target] using greater as source on (
[Target].npi = [Source].npi
and [Target].provider = [Source].provider
) WHEN MATCHED THEN
UPDATE
SET
[Target].rating = [Source].rating WHEN NOT MATCHED THEN INSERT (provider, npi, type, rating)
VALUES
(
[Source].provider, [Source].npi,
'HHA', [Source].rating
);