有条件地更新临时表,同时为 SQL 中的变量分配新值

发布于 2025-01-20 15:46:00 字数 2923 浏览 0 评论 0原文

我试图通过分配变量来更新(或创建)临时表中的列,并在 SQL Server Management Studio 中有条件地更新该变量。

我编写了以下代码,但出现语法错误:

DECLARE @VARI INT = 0;

UPDATE #test1_b
SET Test_group = @VARI, 
    @VARI = CASE 
                WHEN TARGET_WEB = PREVIOUS 
                    THEN @VARI
                    ELSE @VARI + 1 
            END;

这是我得到的错误:

消息 103010,第 16 级,状态 1,第 15 行
解析错误位于第 3 行,第 7 列:“(”附近的语法不正确。

我很困惑,因为我什至没有在代码中包含任何“(”。我试图遵循以下说明: UPDATE 语句中的内联变量赋值。以下是表的前几行和我的预期结果:

Test_groupTARGET_WEBPREVIOUStimeExpected group
1web 1Na11
1web 2web 122
1web 2web 232
1web 1web 243
1web 3web 153

最终,我尝试按具有连续网站值的行进行分组,并总结用户在离开网站之前在每个网站上停留的总时间这是聚合后的预期结果:

Expected GroupTARGET_WEBTotal time
1web 11
2web 25
3web 14
4web 35

我不知道 SQL 是否允许人们使用 UPDATE 声明是否请帮助。

I am trying to update (or create) a column in a temporary table by assigning a variable and update that variable conditionally as well in SQL Server Management Studio.

I wrote the the following code but got an syntax error:

DECLARE @VARI INT = 0;

UPDATE #test1_b
SET Test_group = @VARI, 
    @VARI = CASE 
                WHEN TARGET_WEB = PREVIOUS 
                    THEN @VARI
                    ELSE @VARI + 1 
            END;

Here is the error I got:

Msg 103010, Level 16, State 1, Line 15
Parse error at line: 3, column: 7: Incorrect syntax near '('.

I am very confused as I don't even include any '(' in my code. I was trying to follow this instruction: Inline Variable Assignment in UPDATE Statement. Below is the first few line and my expected outcome of my table:

Test_groupTARGET_WEBPREVIOUStimeExpected group
1web 1Na11
1web 2web 122
1web 2web 232
1web 1web 243
1web 3web 153

Ultimately I am trying to group by the rows with consecutive webs values and sum up the total time that user stay at each web before they leave the page. Here is the desired outcome after aggregation:

Expected GroupTARGET_WEBtotal time
1web 11
2web 25
3web 14
4web 35

I don't know if SQL allows people update the variables using variable itself in the UPDATE statement or not. Please help. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想你只要分分秒秒 2025-01-27 15:46:00

以下将根据您的采样数据和预期结果为您提供所需的内容:

IF OBJECT_ID(N'tempdb..#T', 'U') IS NOT NULL
    DROP TABLE #T;

CREATE TABLE #T (Test_group INT, TARGET_WEB VARCHAR(5), PREVIOUS VARCHAR(5), time INT, ExpectedGroup INT);

INSERT INTO #T(Test_group, TARGET_WEB, PREVIOUS, time, ExpectedGroup)
VALUES
    (1, 'web 1', 'Na', 1, 1),
    (1, 'web 2', 'web 1', 2, 2),
    (1, 'web 2', 'web 2', 3, 2),
    (1, 'web 1', 'web 2', 4, 3),
    (1, 'web 3', 'web 1', 5, 3);


SELECT  t.NewExpectedGroup,
        t.TARGET_WEB,
        SUM(t.time)
FROM    (   SELECT  Test_group, 
                    TARGET_WEB, 
                    PREVIOUS, 
                    time, 
                    ExpectedGroup,
                    NewExpectedGroup = SUM(CASE WHEN TARGET_WEB = [PREVIOUS] THEN 0 ELSE 1 END) OVER (ORDER BY [time])
            FROM    #T
        ) AS t
GROUP BY t.NewExpectedGroup, t.TARGET_WEB
ORDER BY t.NewExpectedGroup;

它仅使用条件窗口函数来工作,仅在target_web<>时才将1添加到总和。以前的。但是,这里要注意的主要内容是,这需要窗口功能中的某种顺序。在您的示例中,它之所以有效,是因为时间恰好是增量,但是在您的实际情况下,我猜这是不太可能的,您需要使用另一列(大概是某种列创建的时间)指定应用预期分组的顺序。

The following will get you what you want based on your sampled data and expected results:

IF OBJECT_ID(N'tempdb..#T', 'U') IS NOT NULL
    DROP TABLE #T;

CREATE TABLE #T (Test_group INT, TARGET_WEB VARCHAR(5), PREVIOUS VARCHAR(5), time INT, ExpectedGroup INT);

INSERT INTO #T(Test_group, TARGET_WEB, PREVIOUS, time, ExpectedGroup)
VALUES
    (1, 'web 1', 'Na', 1, 1),
    (1, 'web 2', 'web 1', 2, 2),
    (1, 'web 2', 'web 2', 3, 2),
    (1, 'web 1', 'web 2', 4, 3),
    (1, 'web 3', 'web 1', 5, 3);


SELECT  t.NewExpectedGroup,
        t.TARGET_WEB,
        SUM(t.time)
FROM    (   SELECT  Test_group, 
                    TARGET_WEB, 
                    PREVIOUS, 
                    time, 
                    ExpectedGroup,
                    NewExpectedGroup = SUM(CASE WHEN TARGET_WEB = [PREVIOUS] THEN 0 ELSE 1 END) OVER (ORDER BY [time])
            FROM    #T
        ) AS t
GROUP BY t.NewExpectedGroup, t.TARGET_WEB
ORDER BY t.NewExpectedGroup;

It works simply using a conditional windowed function, adding 1 to the sum only if TARGET_WEB <> PREVIOUS. The main thing to note here though, is that this requires some kind of order within the windowed function. In your example it works because time happens to be incremental, but in your actual scenario I'd guess this is very unlikely to be the case, and you'll need to use another column (presumably some kind of created time) to specify the order in which to apply your expected grouping.

人生戏 2025-01-27 15:46:00

看来您正在尝试使用变量 @vari 更新字段 test_n ,同时更新变量 @vari 的值。我建议更新字段而不是变量。参考下面的代码。

DECLARE @VARI INT = 0;

UPDATE #test1_b
SET test_n = CASE 
                WHEN TARGET_NUM = PREVIOUS 
                    THEN @VARI
                ELSE @VARI + 1 
             END;

It seems You are trying to update field test_n with variable @vari and at the same time updating value of variable @vari. I suggest to update field instead of variable. Refer below code.

DECLARE @VARI INT = 0;

UPDATE #test1_b
SET test_n = CASE 
                WHEN TARGET_NUM = PREVIOUS 
                    THEN @VARI
                ELSE @VARI + 1 
             END;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文