简体   繁体   中英

sql group by with double conditions

I need to get the amount of distinct parent_ids that fill in one of the conditions below, grouped by day: parent_ids that have both status = pending & processing OR parent_ids who have both status = canceled and processing.

I ve tried something similar to:

SELECT count(parent_id) as pencan, created_at, DATE_FORMAT(a.created_at, '%Y') AS year_key, DATE_FORMAT(a.created_at, '%m-%d') as day_key
FROM sales_flat_order_status_history 
where created_at BETWEEN '2010-01-01 00:00:00' AND '2013-04-30 23:59:59'
GROUP BY created_at ,parent_id
HAVING SUM(status = 'processing')
AND SUM(status IN ('pending', 'cancelling'))

在此处输入图像描述

I think you just need to fix the group by :

SELECT DATE(created_at), count(parent_id) as pencan
FROM sales_flat_order_status_history 
where created_at >= '2010-01-01' AND
      created_at < '2013-05-01'
GROUP BY DATE(created_at) , parent_id
HAVING SUM(status = 'processing') AND
       SUM(status IN ('pending', 'cancelling'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM