仅包括最少6个月的活动

问题描述 投票:0回答:1

我如何只包括公司b2b_id,最低活动为每月1发票,为期6个月。

SELECT
  b2b_id,
  uid,
  issue_date),
  due_date,
  COUNT(*) AS cnt
FROM t_invoice
GROUP BY b2b_id, last_day(issue_date)
HAVING cnt >= 1
mysql sql
1个回答
0
投票

利用另一个条件的having子句。请注意,select子句中不必存在聚合使用。

SELECT
      b2b_id
    , uid
    , issue_date
    , due_date
    , COUNT(*) AS cnt
FROM t_invoice
GROUP BY
      b2b_id
    , last_day(issue_date)
HAVING cnt >= 1
AND COUNT(DISTINCT (case when issue_date >= (current_date - interval 6 month) then month(issue_date) end)) = 6

不确定使用哪个日期列,我选择了issue_date

  • 如果issue_date> =今天减去6个月,则返回该月份数
  • 计算不同的月份数
  • 如果是6,那么在过去的6个月中每月至少有一个isse_date
© www.soinside.com 2019 - 2024. All rights reserved.