我如何只包括公司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
利用另一个条件的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