简体   繁体   中英

MySQL Count from Joined Table on multiple criteria

This is my first post, thank you in advance for your patience and thank you to everyone for participating on Stack, I have used it so much to learn! Please excuse if this was answered before, I have done my best to search for alternate solutions. So I have two tables, an orders table and a customer_history table (This is fake data written in the same format of those tables).

orders

orders_id  customers_id  date_purchased       order_total
12341      12            2016-05-15 00:00:01  24.50
12342      13            2016-06-01 00:00:01  29.99
12343      12            2016-06-01 00:00:01  18.50 

customers_history

id  customers_id  created_at           agent_name  contact_type
1   12            2016-05-31 00:00:01  William     Phone Call
2   12            2016-05-29 00:00:01  Kyle        Email
3   13            2016-05-17 00:00:01  William     Phone Call
4   13            2016-05-28 00:00:01  William     Email
5   12            2016-05-11 00:00:01  Kyle        Email
6   12            2016-05-12 00:00:01  Kyle        Email
5   12            2016-05-13 00:00:01  William     Phone Call

What we're trying to do is build a report card for orders to see if customers are placing an order within 7 days of agent contact. So what I'm wanting to return is:

orders_id  customers_id  date_purchased       order_total  agent_name  phone_count  email_count
12341      12            2016-05-15 00:00:01  24.50        Kyle        0            2
12341      12            2016-05-15 00:00:01  24.50        William     1            0
12342      13            2016-06-01 00:00:01  29.99        William     0            1
12343      12            2016-06-01 00:00:01  18.50        William     1            0
12343      12            2016-06-01 00:00:01  18.50        Kyle        0            1

So far I've got this for my select query but it isn't returning the results I am looking for:

SELECT 
  oc.orders_id, 
  oc.customers_id, 
  oc.date_purchased, 
  oc.order_total,
  SUM(CASE WHEN (ch.contact_type = "Phone Call" AND ch.created_at <= oc.date_purchased AND ch.created_at > (oc.date_purchased - INTERVAL 7 DAY)) THEN 1 ELSE 0 END) AS phone_count,
  SUM(CASE WHEN (ch.contact_type = "Email" AND ch.created_at <= oc.date_purchased AND ch.created_at > (oc.date_purchased - INTERVAL 7 DAY)) THEN 1 ELSE 0 END) AS email_count
FROM orders oc
LEFT JOIN customers_history ch
ON ch.customers_id = oc.customers_id
WHERE ch.agent_name != NULL

Any help is greatly appreciated!

I think you should move the date condtion on where clause (and not in when) and use date_sum and date() format

SELECT 
  oc.orders_id, 
  oc.customers_id, 
  oc.date_purchased, 
  oc.order_total,
  oc.agent_name
  SUM(CASE WHEN (ch.contact_type = "Phone Call"  THEN 1 ELSE 0 END) AS phone_count,
  SUM(CASE WHEN (ch.contact_type = "Email"  THEN 1 ELSE 0 END) AS email_count
FROM orders oc
LEFT JOIN customers_history ch
ON ch.customers_id = oc.customers_id
WHERE ch.agent_name != NULL
AND date(ch.created_at)  <= date(oc.date_purchased)  
AND ch.created_at > DATE_SUB(oc.date_purchased,INTERVAL 7 DAY)
 SELECT t.orders_id,
   t.customers_id,
   t.date_purchased,
   t.order_total,
   h.agent_name,
   (SELECT Count(1)
    FROM   customers_history h2
    WHERE  h2.contact_type = 'Phone Call'
           AND h2.agent_name = h.agent_name
           AND Date(h2.created_at) <= Date(t.date_purchased)
           AND h2.created_at > Date_sub(t.date_purchased, INTERVAL 7 day))
   AS
   phone_count,
   (SELECT Count(1)
    FROM   customers_history h2
    WHERE  h2.contact_type = 'Email'
           AND h2.agent_name = h.agent_name
           AND Date(h2.created_at) <= Date(t.date_purchased)
           AND h2.created_at > Date_sub(t.date_purchased, INTERVAL 7 day))
   AS email_count
FROM   orders t, customers_history h 
WHERE  h.customers_id = t.customers_id
GROUP  BY t.customers_id  

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