MYSQL ROW_NUMBER
我正在使用 row_number 函数按分区排名,但我尝试在 IF 语句中使用 row_number 的结果,但没有成功。
我的代码:
ROW_NUMBER() OVER (partition by wboi.ce19.store, dbn.ce19.COD, dbn.ce19.MOD ORDER by dbn.ce19.store, dbn.ce19.data, dbn.ce01.COD) AS ROW1,
IF('ROW1' = 1, 0, 1) as TEST
但是“TEST”列中的所有值都变为 1,即使 row_number 为 1。
此外,我尝试使用 Row_number() 求和,例如:
'ROW1' + 1 AS TEST2
但所有值都是 1。
因此,ROW_NUMBER() 函数输出是一个数字,或者我需要在任何条件函数中使用它之前将其转换为数字?
I'm using row_number function to rank by partition but I'm trying to use the result of row_number in a IF statement without success.
My code:
ROW_NUMBER() OVER (partition by wboi.ce19.store, dbn.ce19.COD, dbn.ce19.MOD ORDER by dbn.ce19.store, dbn.ce19.data, dbn.ce01.COD) AS ROW1,
IF('ROW1' = 1, 0, 1) as TEST
But all the values in "TEST" column are getting 1, even if the row_number is 1.
Also, I tried to make sum with the Row_number() like:
'ROW1' + 1 AS TEST2
But all the values is 1.
So, ROW_NUMBER() function output is a number or I need to convert it to number before use it in any conditional function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
'ROW1'
不是列ROW1
,它是一个 字符串文字。单引号用于分隔字符串。要分隔标识符,请使用反引号。
在数字上下文中使用字符串文字会将字符串转换为其整数值,即前导数字(如果有)或 0。
因此
'ROW1' + 1
等于0 + 1
或1
。除此之外,您不能进行引用同一选择列表中的列别名的 SQL 查询。换句话说,这是一个错误:
您必须将其包装在子查询或 CTE 中:
'ROW1'
is not the columnROW1
, it's a string literal.The single-quotes are used to delimit strings. To delimit identifiers, use back-ticks.
Using a string literal in a numeric context converts the string to its integer value, which is the leading digits (if any) or else 0.
So
'ROW1' + 1
is equal to0 + 1
or1
.Besides that, you cannot make an SQL query that references a column alias in the same select-list. In other words, this is an error:
You would have to wrap it in a subquery or a CTE: