我正在Azure Synapse中创建一个SQL脚本,以将维度数据从Staging表[Source]转换到其Storage表[Target]。
使用ROW_NUMBER()函数创建带有键的目标:
IF NOT EXISTS (SELECT * FROM sys.tables t INNER JOIN sys.schemas s ON t.schema_id = s.schema_id WHERE s.name = 'prd' and t.name = 'dim_stations')
CREATE TABLE prd.dim_stations
WITH
(
DISTRIBUTION = HASH(station_key),
CLUSTERED COLUMNSTORE INDEX
)
AS SELECT
ROW_NUMBER() OVER(ORDER BY [station_id],[station_name],[station_latitude],[sattion_longitude] ASC) AS station_key,
[station_id],
[station_name] AS name,
[station_latitude] AS latitude,
[sattion_longitude] AS longitude
FROM
stg.stations;
GO
我试图使用MERGE
处理表中的SCD类型1,但我无法处理Insert
语句中的键,错误表示Window Functions Could Only Be Used with SELECT
语句。Merge
语句:
MERGE INTO prd.dim_stations AS p
USING stg.stations AS s
ON s.station_id = p.station_id
WHEN MATCHED
THEN UPDATE SET
p.latitude = s.station_latitude,
p.longitude = s.sattion_longitude,
p.name = s.station_name
WHEN NOT MATCHED BY TARGET THEN
INSERT (station_key, station_id, name, latitude, longitude)
VALUES
(
ROW_NUMBER() OVER(ORDER BY [station_id],[station_name],[station_latitude],[sattion_longitude] ASC),
s.station_id,
s.station_name,
s.station_latitude,
s.sattion_longitude
)
WHEN NOT MATCHED BY SOURCE THEN
DELETE
OPTION(LABEL= 'MERGE Stations')
我真的很感激你能提供的任何帮助。
1条答案
按热度按时间f8rj6qna1#
我已经尝试过这种方法,但没有在Merge中使用SELECT语句。
例如,我创建了两个表,分别称为Source01和Target01,以实现SCD Type 01,它将执行更新和插入。来源:
Target Create语句:
我在ID列上使用ROW_NUMBER()函数。在执行Merge语句之前,行计数为14。
现在在source01我正在更新2条记录。插入1行。
在更新Source表之前
使用MERGE语句。更新目标表中2条记录,插入新行。
合并声明:
更新后