有条件地更新临时表,同时为 SQL 中的变量分配新值
我试图通过分配变量来更新(或创建)临时表中的列,并在 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_group | TARGET_WEB | PREVIOUS | time | Expected group |
---|---|---|---|---|
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 |
最终,我尝试按具有连续网站值的行进行分组,并总结用户在离开网站之前在每个网站上停留的总时间这是聚合后的预期结果:
Expected Group | TARGET_WEB | Total time |
---|---|---|
1 | web 1 | 1 |
2 | web 2 | 5 |
3 | web 1 | 4 |
4 | web 3 | 5 |
我不知道 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_group | TARGET_WEB | PREVIOUS | time | Expected group |
---|---|---|---|---|
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 |
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 Group | TARGET_WEB | total time |
---|---|---|
1 | web 1 | 1 |
2 | web 2 | 5 |
3 | web 1 | 4 |
4 | web 3 | 5 |
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下将根据您的采样数据和预期结果为您提供所需的内容:
它仅使用条件窗口函数来工作,仅在
target_web<>时才将1添加到总和。以前的
。但是,这里要注意的主要内容是,这需要窗口功能中的某种顺序。在您的示例中,它之所以有效,是因为时间
恰好是增量,但是在您的实际情况下,我猜这是不太可能的,您需要使用另一列(大概是某种列创建的时间)指定应用预期分组的顺序。The following will get you what you want based on your sampled data and expected results:
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 becausetime
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.看来您正在尝试使用变量 @vari 更新字段 test_n ,同时更新变量 @vari 的值。我建议更新字段而不是变量。参考下面的代码。
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.