Oracle SQL查询计数,通过内连接分组?

时间:2012-04-18 22:30:01

标签: sql oracle count

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

所以我想知道每个小组有多少辆汽车。

我正在使用此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"

我尝试使用group_bycount,但似乎无法让它工作

1 个答案:

答案 0 :(得分:5)

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