【问题标题】:Intersect keeping the count from both queries for ordering purpose相交保留两个查询的计数以进行排序
【发布时间】:2018-07-18 05:07:01
【问题描述】:

我有两个表,我需要对每个表运行查询。然后需要根据在两个表中找到的名称的出现次数对这两个查询的输出进行交叉和排序。

例如:

查询 1:

select * from userA
group by name
order by count(name) desc

---------------------------
ID  |  name  | count(name)
---------------------------
 1  |  John  |  2
 2  |  Mike  |  1
 3  |  Laura |  1
---------------------------

查询 2:

select * from userB
group by name
order by count(name) desc

---------------------------
ID  |  name  | count(name)
---------------------------
 3  |  Laura |  3
 1  |  John  |  1
 5  |  Peter |  1
---------------------------

查询 3:

select * from userA
group by name
order by count(name) desc
intersect
select * from userB
group by name
order by count(name) desc

---------------------------
ID  |  name   | count(name)
---------------------------
 1  |  John   |  1
 3  |  Laura  |  1
---------------------------

问题是 intersect 将重新运行 count 函数,丢弃每个查询运行的计数。我想看到的是以下输出:

---------------------------
ID  |  name   | count(name)
---------------------------
 3  |  Laura  |  4
 1  |  John   |  3
---------------------------

有谁知道如何做到这一点?

【问题讨论】:

    标签: sqlite intersect


    【解决方案1】:

    假设你的数据集是这样的:

    用户A

    id          name      
    ----------  ----------
    1           John      
    1           John      
    2           Mike      
    3           Laura
    

    用户B

    id          name      
    ----------  ----------
    1           John      
    3           Laura     
    3           Laura     
    3           Laura     
    5           Peter
    

    您可以编写这样的查询以获得所需的结果

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

    结果

    id          name        count(*)  
    ----------  ----------  ----------
    1           John        3         
    3           Laura       4         
    

    说明

    合并数据集,因为您想知道两个表中 John 和 Laura 的数量,合并。 Union All 将允许保留两个表中的重复项。组合时,请记住来源。

    select id, name, 'A' as source from userA
    union all
    select id, name, 'B' from userB
    

    上面的查询将结合两者的数据并为您提供以下结果:

    id          name        source    
    ----------  ----------  ----------
    1           John        A         
    1           John        A         
    2           Mike        A         
    3           Laura       A         
    1           John        B         
    3           Laura       B         
    3           Laura       B         
    3           Laura       B         
    5           Peter       B
    

    现在,让我们只提取来自两个表的记录。因此,我们按 id 和 name 分组并使用having 子句提取count(distinct source) = 2。这意味着,给我有 2 个来源的记录。劳拉和约翰恰好在两张桌子上。

    选择数据时,我们要求count(*)获取id+name组合的记录数。

    【讨论】:

    • 感谢 zedfoxus!我在这里发布了另一个与此相关的问题:stackoverflow.com/questions/51645512/…
    • @M.Ridha 如果答案对您有帮助,您能否访问您过去提出的问题并将答案标记为已接受?如果您需要帮助将答案标记为已接受,请告诉我。一旦你这样做了,让我知道,我可以看看你的新问题。
    • @M.Ridha 没问题。如果答案对您有所帮助,我强烈建议您回到过去提出的其他问题并将它们全部标记为已回答。这是一种感谢那些花时间回答你问题的人的方式。如果您喜欢这个答案,您可以选择给他们投票。
    猜你喜欢
    • 2021-04-07
    • 2017-03-29
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2012-04-12
    • 2016-01-02
    • 1970-01-01
    • 2015-06-23
    相关资源
    最近更新 更多