根据两个表的共同条件查询并根据出现次数排序

Query based on common criteria from two tables and sort based on number of occurrences

我有以下表格,我需要对每个表格进行 运行 查询。

用户A

id          name         title
----------  ----------   --------
1           john         engineer
1           John         engineer
2           mike         designer
3           laura        manager
4           dave         engineer

用户B

id          name         title
----------  ----------   --------
1           john         engineer
3           laura        manager
3           laura        manager   
3           laura        Manager   
5           Peter        sales
4           Dave         engineer

并且我正在使用以下查询来 grep 在两个表中找到的名称(相交)并根据找到的出现次数进行排序:

select id, name, title, count(*)
from (
    select id, name, title, 'A' as source from userA
    union all
    select id, name, title, 'B' from userB
) 
group by id, name
having count(distinct source) = 2;

以上查询的输出:

id          name         title        count(*)
----------  ----------   --------     --------
1           john         engineer     3
3           laura        manager      4
4           dave         engineer     2

现在我想弄清楚如何构建一个查询以仅显示每个标题类别的最多计数,因此在上面的示例中,只有 john 应该显示在工程师类别中,因为他的计数最多对于那个类别。 基本上,我想显示以下输出:

id          name         title        count(*)
----------  ----------   --------     --------
1           john         engineer     3
3           laura        manager      4

任何人都可以帮助如何做到这一点?

谢谢!

试试这个:

创建一个 VIEW 合并来自两个表的用户。

CREATE VIEW userA_B as
  select *, 'A' as source from userA
  union all
  select *, 'B' as source from userB;

此视图中的数据

select * from userA_B;

id          name        title       source    
----------  ----------  ----------  ----------
1           john        engineer    A         
1           john        engineer    A         
2           mike        designer    A         
3           laura       manager     A         
4           dave        engineer    A         
1           john        engineer    B         
3           laura       manager     B         
3           laura       manager     B         
3           laura       manager     B         
5           peter       sales       B         
4           dave        engineer    B

创建一个 VIEW,只显示出现在两个表中的那些用户。

CREATE VIEW user_in_both_A_B as
  select id, name, title, count(*) as total_appearance
  from userA_B
  group by id, name, title
  having count(distinct source) = 2;

此视图中的数据

select * from user_in_both_A_B;

id          name        title       total_appearance
----------  ----------  ----------  ----------------
1           john        engineer    3               
3           laura       manager     4               
4           dave        engineer    2

创建一个 VIEW 显示出现次数最多的标题。

CREATE VIEW title_appearing_most as
  select title, max(total_appearance) as max_total_appearance
  from user_in_both_A_B
  group by title

此视图中的数据

select * from title_appearing_most;

title       max_total_appearance
----------  --------------------
engineer    3                   
manager     4

现在,仅从 user_in_both_A_B 视图中获取标题和出现次数在 title_appearing_most 中匹配的那些记录。

select ab.*
from user_in_both_A_B ab
inner join title_appearing_most m
    on ab.title = m.title
    and ab.total_appearance = m.max_total_appearance;

最终结果

id          name        title       total_appearance
----------  ----------  ----------  ----------------
1           john        engineer    3               
3           laura       manager     4               

视图将帮助您存储可以按需执行的查询,并且名称更短。 Sub-queries里面的sub-queries可以在视觉上避开,阅读更简单。