[英]mySQL Select where results from column “b” have column “a” in common
This one is kinda hard to explain, I'll give it a shot. 这个有点难以解释,我会试一试。
I have this table where one of the columns is the type column. 我有这个表,其中一列是类型列。 The salesperson will insert records that will contain a b_id and also an action_id.
销售人员将插入包含b_id和action_id的记录。
with the following code I retrieve some info, 使用以下代码我检索一些信息,
SELECT entry_type, COUNT(DISTINCT(b_name)) AS '# of prospects',
SUM(case when entries.out_id = '1' then 1 else 0 end) 'No Interest',
SUM(case when entries.out_id = '2' then 1 else 0 end) 'Needs Follow Up',
SUM(case when entries.out_id = '3' then 1 else 0 end) 'Appointment Booked'
FROM entries
LEFT JOIN outcomes on outcomes.out_id = entries.out_id
LEFT JOIN type on type.type_id = entries.type_id
LEFT JOIN business on entries.b_id = business.b_id
LEFT JOIN users on users.user_id = entries.user_id
WHERE b_name LIKE 'July%' AND (entries.type_id = 1 OR entries.type_id = 2 OR entries.type_id = 14)
GROUP BY entry_type;
The result is the following 结果如下
ACTION # OF PROSPECTS NO INTEREST NEEDS FOLLOW UP APP. BOOKED
Call 4 1 2 1
Follow Up Contact 2 0 0 2
Walk In 1 1 0 0
The thing is, There are 2 possible initial actions, "Call" or "Walk In". 问题是,有2个可能的初始动作,“呼叫”或“走进来”。 "Follow Up Contact" is used if necessary after a initial call or walk in. As you can see, I have 2 appointments booked originated from this follow up.
如果有必要,可以在初次通话或步行后使用“跟进联系”。如您所见,我有2个预约来自此跟进。 Here is the question.
这是个问题。 How do I know if this follow up contact is related to an initial call or an initial walk in?
我怎么知道这次跟进联系是否与初次通话或初次步行有关?
I need to be able to generate a report specifying how many appointments were originated from each type of approach ( call or walk in ). 我需要能够生成一个报告,指定从每种方法(呼叫或步入)发起的约会数量。
Thanks in advance 提前致谢
Use a self-join: 使用自联接:
SELECT e1.type AS original_type, COUNT(e2.b_id) AS count
FROM entries AS e1
LEFT JOIN entries AS e2 ON e2.b_id = e1.b_id AND e2.entry_type = 'Follow Up Contact'
WHERE e1.entry_type IN ('Call', 'Walk In')
GROUP BY original_type
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.