基于两个表中的通用条件进行查询,并根据出现次数进行排序

时间:2018-08-02 04:28:34

标签: sqlite join intersect

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

userA

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

userB

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

有人可以帮忙吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

尝试一下:

创建一个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               

视图将帮助您存储可以按需执行且名称较短的查询。子查询中的子查询可以从视觉上避免,从而使阅读更加简单。