在雪花中的条款中
我正在尝试在Snowflake中的Where子句中做一个案例语句,但我不太确定该怎么做。
我要做的是,如果我当前的一个月是1月,那么日期的哪个子句是上一年和今天的开始。如果不是,那么日期的where子句将在当年年初到今天。
WHERE
CASE MONTH(CURRENT_DATE()) = 1 THEN DATE BETWEEN DATE_TRUNC(‘YEAR’, DATEADD(YEAR, -1, CURRENT_DATE())) AND CURRENT_DATE()
CASE MONTH(CURRENT_DATE()) != 1 THEN DATE BETWEEN DATE_TRUNC(‘YEAR’, CURRENT_DATE()) AND CURRENT_DATE()
END
感谢任何帮助!
I am trying to do a case statement within the where clause in snowflake but I’m not quite sure how should I go about doing it.
What I’m trying to do is, if my current month is Jan, then the where clause for date is between start of previous year and today. If not, the where clause for date would be between start of current year and today.
WHERE
CASE MONTH(CURRENT_DATE()) = 1 THEN DATE BETWEEN DATE_TRUNC(‘YEAR’, DATEADD(YEAR, -1, CURRENT_DATE())) AND CURRENT_DATE()
CASE MONTH(CURRENT_DATE()) != 1 THEN DATE BETWEEN DATE_TRUNC(‘YEAR’, CURRENT_DATE()) AND CURRENT_DATE()
END
Appreciate any help on this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
案例
返回-1
的表达> dateadd()在date_trunc()
:Use a
CASE
expression that returns-1
if the current month is January or0
for any other month, so that you can get withDATEADD()
a date of the previous or the current year to use inDATE_TRUNC()
:我怀疑您甚至不需要在此处使用
案例
:I suspect that you don't even need to use
CASE
here:因此,其他答案还不错,但是...答案可以更简单地
制作一个小桌子来制动正在发生的事情。
this shows:
您的逻辑要求“是1月份的当前日期”,此时为上一年,然后日期截断为年度,否则,将当前日期截断为年。作为两次测试的开始。
这与当前日期减去一个月相同,然后将其截断为年。
因此,无需任何IFF或案例
,如果您想放弃某些paren,
current_date
如果将其留在上情况下,则可以使用它,因此它甚至可以较小:So the other answers are quite good, but... the answer can be even simpler
Making a little table to brake down what is happening.
this shows:
Your logic is asking "is the current date in the month of January", at which point take the prior year, and then date truncate to the year, otherwise take the current date and truncate to the year. As the start of a BETWEEN test.
This is the same as getting the current date subtracting one month, and truncating this to year.
Thus there is no need for any IFF or CASE
and if you like to drop some paren's,
CURRENT_DATE
can be used if you leave it in upper case, thus it can even be smaller: