简体   繁体   English

mySQL选择列“b”的结果共有列“a”的位置

[英]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.

相关问题 MySQL:如何选择A列不在B列的位置? - MySQL: How to select where column A not in column B? 无法使用表 b 中的值更新表 a 中的列,其中两者都有一个公共列 - unable to update column in table a with values from table b where both have a common column MySQL Select 将 A 列与 B 列存在的表不同,或者如果 B 列不存在,则基于 C 列的最新 - MySQL Select distinct column A from table where column B exists or if none with column B then latest based on column C 选择B列上的总和,其中A列是相同的mysql - select sum on column B where column A is the same mysql MySQL选择A列中的所有X,其中B列= Y - mysql select all X in column A where column B = Y MySQL选择将结果拆分到不同的列 - MySQL Select Where spliting results into different column mysql select * from table from table where column a value = column b value laravel - mysql select * from table where column a value = column b value laravel MySQL从列a中为每个不同的列b选择所有不同的值,其中计数不同的a> 1 - MySQL select all distinct values from column a for each distinct column b, where count distinct a >1 在mysql select语句中将公用列值作为所有结果的标题 - Make a common column value as the heading of all results in mysql select statement 从表中选择A列或B列中的值 - Select from table where value in column A or column B
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM