如何在MERGE语句中使用窗口函数?

huangapple go评论60阅读模式
英文:

How to use Window Function in MERGE statement?

问题

我正在在Azure Synapse中创建一个SQL脚本,将维度数据从暂存表[Source]转换到存储表[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 Type 1,但在Insert语句中无法处理键,出现错误,意味着窗口函数只能与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')

我非常感谢您提供的任何帮助。

英文:

I'm creating a SQL Script in Azure Synapse to transform dimension data from a Staging table [Source] to its Storage table [Target].

The target is created with a key using ROW_NUMBER() function:

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

I'm trying to handle SCD Type 1 in the table using MERGE but I can't handle the key in the Insert Statement with an error that means Window Functions Could Only be Used with SELECT Statement.

The Merge Statement:

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')

I really appreciate any help you can provide.

答案1

得分: 0

我尝试过在合并中不使用SELECT语句来尝试这种方法。

例如,我创建了两个表,分别称为Source01和Target01,以实现SCD Type 01,该类型将执行更新和插入。

Source:

CREATE TABLE Source01 (
id INT,
name VARCHAR(50),
age INT,
city VARCHAR(50)
);

Target 创建语句:

CREATE TABLE Target01 (
id INT,
name VARCHAR(50),
age INT,
city VARCHAR(50),
row_num INT,
CONSTRAINT PK_Target01 PRIMARY KEY NONCLUSTERED (id) NOT ENFORCED
) WITH (
DISTRIBUTION = HASH(id),
CLUSTERED COLUMNSTORE INDEX
);  
INSERT INTO Target01 (id, name, age, city, row_num)
SELECT
id,
name,
age,
city,
ROW_NUMBER() OVER (ORDER BY id) AS row_num
FROM Source01;

在目标表中,我只是在ID列上使用ROW_NUMBER()函数。在执行合并语句之前,行数为14。

如何在MERGE语句中使用窗口函数?

现在在Source01中,我更新了2条记录,并插入了1行。

-- 更新 John Doe 的年龄和城市
UPDATE Source01
SET age = 26,
city = 'Chicago'
WHERE id = 1;
-- 更新 Jane Smith 的年龄和城市
UPDATE Source01
SET age = 33,
city = 'San Francisco'
WHERE id = 2;
-- 在源表中插入一条新记录
INSERT INTO Source01 VALUES(16, 'Samantha Green', 31, 'Austin');

在更新Source表之前:

如何在MERGE语句中使用窗口函数?
如何在MERGE语句中使用窗口函数?

使用MERGE语句。在目标表中更新了2条记录,并插入了新行。

MERGE 语句:

MERGE INTO Target01 AS T
USING Source01 AS S
ON T.id = S.id
WHEN MATCHED THEN
UPDATE SET
T.name = S.name,
T.age = S.age,
T.city = S.city
WHEN NOT MATCHED THEN
INSERT (id, name, age, city)
VALUES (S.id, S.name, S.age, S.city);

在更新后:

如何在MERGE语句中使用窗口函数?
如何在MERGE语句中使用窗口函数?
如何在MERGE语句中使用窗口函数?

英文:

I have tried this approach with out using the SELECT statement in the Merge.

For example I have created 2 table Called the Source01 and Target01 to implement SCD Type 01 which will perform the update and Insert.
Source:

CREATE  TABLE Source01 (
id INT,

name VARCHAR(50),

age INT,

city VARCHAR(50)

);

Target Create statement:

CREATE  TABLE Target01 (
id INT,
name VARCHAR(50),
age INT,
city VARCHAR(50),
row_num INT,
CONSTRAINT PK_Target01 PRIMARY  KEY  NONCLUSTERED (id) NOT ENFORCED
) WITH (
DISTRIBUTION = HASH(id),
CLUSTERED COLUMNSTORE INDEX
);  
INSERT  INTO Target01 (id, name, age, city, row_num)SELECT
id,

name,

age,

city,

ROW_NUMBER() OVER (ORDER  BY id) AS row_num

FROM Source01;

I am using the ROW_NUMBER() function just over ID column. in the target table.Before perform the Merge statement row count 14.
如何在MERGE语句中使用窗口函数?

Now at the source01 I am updating 2 records. and Inserting 1 row.

-- Update age and city for John Doe
UPDATE Source01
SET age = 26,
city = 'Chicago'
WHERE id = 1;
-- Update age and city for Jane Smith
UPDATE Source01
SET age = 33,
city = 'San Francisco'
WHERE id = 2;
-- Insert a new record in the Source table
INSERT  INTO Source01 VALUES(16, 'Samantha Green', 31, 'Austin');

如何在MERGE语句中使用窗口函数?
如何在MERGE语句中使用窗口函数?

Before the Source table is updated
如何在MERGE语句中使用窗口函数?
如何在MERGE语句中使用窗口函数?
Using the MERGE statement. Update 2 records in the Target table and Inserting new row.

MERGE Statement:

MERGE  INTO Target01 AS T
USING Source01 AS S
ON T.id = S.id
WHEN  MATCHED  THEN
UPDATE  SET
T.name = S.name,
T.age = S.age,
T.city = S.city
WHEN  NOT  MATCHED  THEN
INSERT (id, name, age, city)
VALUES (S.id, S.name, S.age, S.city);

如何在MERGE语句中使用窗口函数?
After Update
如何在MERGE语句中使用窗口函数?
如何在MERGE语句中使用窗口函数?

huangapple
  • 本文由 发表于 2023年6月6日 16:06:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定