Oracle SQL 查询计数,使用内部联接分组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10219041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:44:13  来源:igfitidea点击:

Oracle SQL query count, group by with an innerjoin?

sqloraclecount

提问by user673906

Basicly I have a bunch of Cars that belong to dealers and the dealers have a group. The dealer table has the GROUP.ID and the Group table has the group name. A Group has multiple dealers

基本上我有一堆属于经销商的汽车,经销商有一个小组。经销商表具有 GROUP.ID,组表具有组名称。一个集团有多个经销商

So I want to count how many cars each group has.

所以我想数一数每组有多少辆车。

I am using this atm

我正在使用这个自动取款机

select  
  (select GROUP_NAME from "GROUP" where "GROUP".GROUP_ID = "DEALER"."GROUP_ID" ),
  "DEALER"."GROUP_ID" as "DEALER GROUP ID",
  "DEALER"."DEALER_NAME" as "DEALER DEALER NAME",
  "CAR"."CAR_DEALER" as "CAR DEALER"         
from 
  "CAR"
INNER JOIN 
  DEALER
ON 
  "DEALER"."DEALER_NAME" ="CAR"."CAR_DEALER"

I tried using group_byand countbut I can't seem to get it to work

我尝试使用group_bycount但似乎无法正常工作

回答by GolezTrol

select
  g.GROUP_NAME,
  g.GROUP_ID,
  count(*) as CAR_COUNT
from
  GROUP g
  inner join DEALER d on d.GROUP_ID = g.GROUP_ID
  inner join CAR c on c.DEALER_ID = d.DEALERID
group by
  /* Also add here all field you want to select from GROUP */
  g.GROUP_NAME,
  g.GROUPID