AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 261573
Accepted
Anders Lindén
Anders Lindén
Asked: 2020-03-11 00:52:24 +0800 CST2020-03-11 00:52:24 +0800 CST 2020-03-11 00:52:24 +0800 CST

用不相关的计数分组

  • 772

我有三张桌子,一张,两张和三张。

一

表一内容

二

表二内容

三

表三内容

有了这个查询

select one.id, count(two.id) as num_twos, count(three.id) as num_threes from one
left join two on one.id = two.one_id
left join three on one.id = three.one_id
group by one.id

我希望得到

1 2 1
2 1 4
3 0 0
4 0 0

因为有

  • 二分之二的记录与 one_id 1 相关,三分之二的记录与 one_id 1 相关。
  • 与 one_id 2 相关的二分之一记录和与 one_id 2 相关的三分之四记录。

但我明白了

结果

为什么?计数似乎成倍增加。我可以在没有子查询的情况下获得所需的行为吗?

t-sql group-by
  • 1 1 个回答
  • 33 Views

1 个回答

  • Voted
  1. Best Answer
    Sabin B
    2020-03-11T00:59:38+08:002020-03-11T00:59:38+08:00

    图像这个:

    select one_id,count(id) as num_Twos
    from Two
    group by one_id
    

    输出:

    one_id      num_Twos
    1           2
    2           1
    

    然后加入主表(这样,表一中的主记录不重复,也不需要 distinct ):

    select
        id, num_Twos, isnull(num_Twos,0) as numTwos
    from One 
        left join
        (
            select one_id,count(id) as num_Twos
            from Two
            group by one_id
        )as two
            ON id =two.one_id
    

    输出:(使用 isnull - 放置 0 而不是 NULL)

    id          num_Twos    numTwos
    1           2           2
    2           1           1
    3           NULL        0
    4           NULL        0
    

    同样的事情,与表 3
    来自评论:这样,您不需要顶级 group by ,因为所有信息已经汇总在() as Twoand() as Three

    或不同的:

    select one.id
            , count(distinct two.id) as num_twos
            , count(distinct three.id) as num_threes 
    from one
    left join two on one.id = two.one_id
    left join three on one.id = three.one_id
    group by one.id
    

    输出 :

    id          num_twos    num_threes
    1           2           1
    2           1           4
    3           0           0
    4           0           0
    

    来源:

    with One 
    as (select 1 id union all select 2 union all select 3 union all select 4)
    ,Two 
    as (select 1 as id, 1 as one_id Union all select 2,1 union all select 3,2)
    ,Three
    as (select 1 as id,1 as one_id union all select 2,2 union all select 3,2 union all select 4,2 union all select 5,2)
    

    此外,您可以将其重写为:

    select id
            , isnull(num_twos,0) as num_twos
            , isnull(num_threes,0) as num_threes
    from one
    left join (select one_id ,count(id) as num_twos from two group by one_id) as two on one.id = two.one_id
    left join (select one_id ,count(id) as num_threes from three group by one_id) as three on one.id = three.one_id
    
    • 1

相关问题

  • 如何使用 TSQL 更改 SQL 服务器配置管理器设置?

  • 如何从结果集中获取列名和类型的列表?

  • MS SQL:使用计算值计算其他值

  • 如何判断 SQL Server 数据库是否仍在使用?

  • 实施 PIVOT 查询

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve