Mysql根据当前月份的上一季度月份​​选择行

时间:2018-01-09 04:07:09

标签: mysql sql

我想查询行以查看基于上一季度月份​​的注册记录,例如:

当前月/年: 2018年1月

查询应按注册月份显示所有记录: 2017年10月 2017年7月 2017年4月等等。

我使用以下查询,但只能选择过去3个月的行。

SELECT name, date, amount, agreement, bank
FROM `account`
WHERE (YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 3 MONTH) AND MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 3 MONTH))

2 个答案:

答案 0 :(得分:0)

你可以像这样做算术:

# create summary data frame from HIST
df <- HIST %>%
  group_by(Fonte, UF) %>%
  summarise(SUMMW = sum(MW))

# calculate total bar height for each UF value, & sort accordingly.
df2 <- df %>%
  group_by(UF) %>%
  summarise(bar.heights = sum(SUMMW)) %>%
  ungroup() %>%
  arrange(desc(bar.heights))

# convert UF in the summary data frame to factor, with levels in the sorted order
df$UF <- factor(df$UF, levels = df2$UF)
rm(df2) # you can remove df2 after this; it's not needed anymore

# plot
ggplot(df, 
       aes(x = UF, y = SUMMW, fill = Fonte))+
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  geom_col()

您似乎理解如何处理年份组件,但类似于:

select a.*
from account a
where mod(month(date), 3) = mod(month(current_date), 3);

答案 1 :(得分:0)

手动试试这个

select *
from account 
Where current_date >= dateadd(year, -1, Getdate())
    And Month(current_date) In (
            Month(Getdate()), 
            (Case When Month(Getdate())+3 > 12 Then Month(Getdate())+3-12 Else Month(Getdate())+3 End), 
            (Case When Month(Getdate())+6 > 12 Then Month(Getdate())+6-12 Else Month(Getdate())+6 End), 
            (Case When Month(Getdate())+9 > 12 Then Month(Getdate())+9-12 Else Month(Getdate())+9 End)
            )
相关问题