mySQL选择列" b"的结果。列#34; a"共同

时间:2016-06-27 18:03:32

标签: mysql

这个有点难以解释,我会试一试。

我有这个表,其中一列是类型列。销售人员将插入包含b_id和action_id的记录。

使用以下代码检索一些信息,

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;

结果如下

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

问题是,有2个可能的初始动作," Call"或者"走进"。 "跟进联系"如果有必要,在初次通话或走进后使用。如您所见,我有2个预约来自此跟进。这是个问题。我怎么知道这次跟进联系是否与初次通话或初次步行有关?

我需要能够生成一个报告,指定从每种方法(呼叫或步入)发起的约会数量。

提前致谢

1 个答案:

答案 0 :(得分:0)

使用自我加入:

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