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
Oracle SQL query count, group by with an innerjoin?
提问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_by
and count
but I can't seem to get it to work
我尝试使用group_by
,count
但似乎无法正常工作
回答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