使用与列名相同的变量名 – Clickhouse

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

Using variable name the same as column name - Clickhouse

问题

我有一个名为mytable的表,其中mydate是其中的一列。
然而,由于SELECT子句是动态的,我需要使用addMinutes预调整列。
因此,使用该函数将列添加到变量中,以便在列出现在SELECT中时,变量的值覆盖表中的实际值。

SELECT mydate FROM "mytable"

返回mydate的原始值

WITH addMinutes(mydate, 300) AS mydate SELECT mydate FROM "mytable"

返回mydate的原始值,预期返回变量值

恰好相反发生了;即使变量被提及(与列的名称相同),实际列值也会覆盖WITH子句。

我们是否有一种方法可以使用WITH子句中与表中列名称相同的变量?

英文:

I have a table mytable and mydate is a column in it.
However, since the SELECT clause is dynamic, I need to pre-adjust the column with addMinutes.
So, the column is added to the variable with the function such that in the event the column turns up in SELECT the variable is taken over than the actual value in the table.

SELECT mydate FROM "mytable"
> original value of mydate is returned

<br/>

WITH addMinutes(mydate,300) AS mydate SELECT mydate FROM &quot;mytable&quot;
> original value of mydate is returned, expected to return variable value

The exact opposite happens; Even if the variable is mentioned (same name as the column), the actual column value overrides the WITH clause.

Do we have a workaround to use WITH clause variables with the same name as the columns in the table?

答案1

得分: 1

SELECT
    * REPLACE addMinutes(mydate, 300) AS mydate, mytable.mydate
FROM mytable
英文:
SELECT
    * REPLACE addMinutes(mydate, 300) AS mydate, mytable.mydate
FROM mytable

答案2

得分: 0

I'm not aware of any workaround. The real question is: Why do you need the WITH clause to use the same variable name?

Everything works as expected if you use a different name:

WITH 
   addMinutes(mydate, 300) AS mynewdate
SELECT 
   mynewdate,
   mydate 
FROM mytable; 

英文:

I'm not aware of any workaround. The real question is: Why do you need the WITH clause to use the same variable name?

Everything works as expected if you use a different name:

WITH 
   addMinutes(mydate, 300) AS mynewdate
SELECT 
   mynewdate,
   mydate 
FROM mytable; 

huangapple
  • 本文由 发表于 2023年2月8日 15:00:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382329.html
匿名

发表评论

匿名网友

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

确定