Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 按联接分组查询_Sql_Oracle - Fatal编程技术网

Sql 按联接分组查询

Sql 按联接分组查询,sql,oracle,Sql,Oracle,我的数据库中有两个表: 应用程序(id、类型、用户id、状态id) 状态(id、名称) 我想按类型分组,以获得每种类型的计数,然后作为第三列,我想得到该计数中有多少个状态为3 大概是这样的: 类型 类型u计数 状态\u 3\u计数 类型1 4. 2. 类型2 10 5. 您可以使用条件聚合,如下所示: select a.type, count(*) type_count, count(case when status = 3 then 1 end) status_3_count

我的数据库中有两个表:

  • 应用程序(id、类型、用户id、状态id)
  • 状态(id、名称) 我想按类型分组,以获得每种类型的计数,然后作为第三列,我想得到该计数中有多少个状态为3

    大概是这样的:

    类型 类型u计数 状态\u 3\u计数 类型1 4. 2. 类型2 10 5.
    您可以使用条件聚合,如下所示:

    select a.type, count(*) type_count, 
           count(case when status = 3 then 1 end) status_3_count
        from applications a
        join statuses b on b.id = a.status_id and b.id = 3
    Group by a.type
    

    您可以通过GROUPBY从应用程序表中获取所有这些信息。请尝试以下查询:

    select type,count(*) type_count,sum(case when status_id=3 then 1 else 0 end)status_3_count
    from applications 
    group by type
    

    非常感谢。这是有效的。但我还有一个问题,如果我想得到另一个表的计数,这个表有join with STATUS表。例如,描述表(id,名称)和状态表(id,名称,描述\u id)。现在我想要同样的东西,但是我想要的不是状态\ 3 \计数,而是描述\ 3 \计数?然后,在case..when中,使用描述\ id=3,而不是状态=3