简体   繁体   English

SQL (Snowflake):我可以用一个虚拟列做另一个虚拟列吗? 如果没有,解决方法是什么?

[英]SQL (Snowflake): Can I use a virtual column to make another virtual column? If not, what's the workaround?

So, I have two columns which are in unix/ epoch format (in milliseconds).所以,我有两列是 unix/epoch 格式(以毫秒为单位)。 I convert them accordingly:我相应地转换它们:

dateadd(S, TIME_START/1000, '1970-01-01') AS J_Start, 

dateadd(S, TIME_END/1000, '1970-01-01') AS J_End, 

I want another column with the differential.我想要另一个带有差异的列。 However, when I try and do J_Start - J_End, I get an invalid identifier error.但是,当我尝试执行 J_Start - J_End 时,我得到一个无效的标识符错误。 Is there any way around this?有没有办法解决? I've also tried substracting the full conversion syntax from one another but to no avail...我也尝试过相互减去完整的转换语法,但无济于事......

I was thinking of creating a virtual table and then joining it to the original but I would still encounter the aforementioned problem: Generating a virtual column from other, pre-existing one(s)我正在考虑创建一个虚拟表,然后将其加入原始表,但我仍然会遇到上述问题:Generating a virtual column from other, pre-existing one(s)

It works for me:这个对我有用:

select 5 as a, 1 as b, a-b as c;
+---+---+---+
| A | B | C |
|---+---+---|
| 5 | 1 | 4 |
+---+---+---+

Unless I did not fully understand the question.除非我没有完全理解这个问题。

If you have your full query with exact error message, it can help.如果您的完整查询带有确切的错误消息,它会有所帮助。

You can use datediff to get the differential.您可以使用datediff获取差异。 I am calculating the diff in days but you can change it as desired.我以天为单位计算差异,但您可以根据需要更改它。 Double quotes wasn't the cause of the error, but you don't really need them.双引号不是错误的原因,但您并不需要它们。 Just use column names that don't collide with reserved keywords.只需使用不与保留关键字冲突的列名。

select datediff(day, dateadd(S, j_start/1000, '1970-01-01'),dateadd(S, j_end/1000, '1970-01-01'))

You could also use simple subtraction, but you'd have to cast the output of dateadd as date first.您也可以使用简单的减法,但您必须先将 dateadd 的dateadddate The one above is easier to maintain and modify though上面那个更容易维护和修改

select dateadd(S, j_start/1000, '1970-01-01')::date - dateadd(S, j_end/1000, '1970-01-01')::date

And if you really want to re-use column alias in the same select, which I am not a fan of, you can do如果你真的想在同一个 select 中重用列别名,我不喜欢,你可以这样做

select dateadd(S, j_start/1000, '1970-01-01')::date as j_start,
       dateadd(S, j_end/1000, '1970-01-01')::date as j_end,
       b-a as diff_days

Having said that, if you only need the diff between two dates expressed in milliseconds, it doesn't matter if you use 1970 or 1800. Just do话虽如此,如果您只需要以毫秒表示的两个日期之间的差异,那么使用 1970 还是 1800 都没有关系。只要做

select floor((time_end-time_start) / (1000 * 60 * 60 * 24)) as diff_days

in snowflake you can reference the named object of the SELECT section in the other sections, and the parse most "gets what you mean", thus in snowflake:在雪花中,您可以在其他部分中引用 SELECT 部分的命名 object,并且解析最“明白你的意思”,因此在雪花中:

SELECT 
     '2021-12-16' AS date_string,
     to_date(date_string) as datetime_a,
     dateadd('day', 1, date_time_a) AS a_day_later,
     date_diff('hours', datetime_a, a_day_later);

is just fine, and will give the results of:很好,并且会给出以下结果:

"2021-12-16", 2021-12-16, 2021-12-17, 24

thus in the SQL I gave you on your location question, I was refering to things just declared.因此,在 SQL 中,我就您的位置问题给了您,我指的是刚刚宣布的事情。

The error you are seeing is the fact the subtraction of date's is not supported, because what format do you want the answer in?您看到的错误是不支持减去日期的事实,因为您想要答案的格式是什么? Thus the use of date_diff因此使用date_diff

We can use existing column aliases and build some calculations.Below is example using your var's:我们可以使用现有的列别名并构建一些计算。下面是使用您的 var 的示例:

  select
    to_timestamp('2021-01-01T00:00:00')::timestamp as "J_Start" ,to_timestamp('2021-02-01T00:00:00')::timestamp as "J_end",
    date_part(epoch_second, "J_Start") as "Epcoh_JSTART",
    date_part(epoch_second, "J_end") as "Epcoh_JEND",
    "Epcoh_JSTART" - "Epcoh_JEND";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM