gpt4 book ai didi

sql - 获取当前月份?

转载 作者:行者123 更新时间:2023-11-29 14:21:33 29 4
gpt4 key购买 nike

在 SQL 查询中,我只需要获取当月的一些数据。要获得特定月份,我会这样做。

select (case when date_part('month', date) = 3 and date_part('year', date) = 2014 then amount end)
,(case when date_part('month', date) = 3 and date_part('year', date) = 2013 then amount end
from <table>

我的主要目的是获取今年的当前月份以及上一年的同月。

这就是我要尝试的

select current_month(case when date_part('year', date) = 2014 then amount end) as current
,(case when current_month and date_part('year', date) = 2013 then amount end) as last_year

最佳答案

您可以使用 now()current_timestamp 获取当前时间,然后是当前月、年和去年:

select (case when date_part('month', date) = extract(month from now()) and date_part('year', date) = extract(year from now()) then amount end)
,(case when date_part('month', date) = extract(month from now()) and date_part('year', date) = extract(year from now())-1 then amount end
from <table>

关于sql - 获取当前月份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24147188/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com