在引用的表中计数和分组,选择其他属性

时间:2017-04-10 15:31:47

标签: sql database oracle count reference

我有两个表:account_table和branch_table。

create table branch_table(
    id int,
    address varchar
);

create table account_table(
    accid int,
    balance int,
    type varchar,
    branch_ref ref branch_type
branch_ref SCOPE IS branch_table
);

如何列出类型'保存'的帐户数量?每个分支,并选择该分支的地址?

2 个答案:

答案 0 :(得分:0)

SELECT b.address,COUNT(*) AS no_of_accts
  FROM account_table a
INNER JOIN 
       branch_table b
    ON a.branch_ref = b.id 
 WHERE a.type = 'saving'
GROUP BY b.address;

答案 1 :(得分:0)

对于每个分支,将有唯一的地址,我们可以在这两个列上进行分组操作,或者我们可以在branchid上进行分组,然后取最大的地址,这不会改变任何东西。并且还需要按帐户类型进行分组以获取类型明确的详细信息。

SELECT b.id, b.address,COUNT(accid) no_of_accts
  FROM account_table a
INNER JOIN 
       branch_table b
    ON a.branch_ref = b.id 
 WHERE a.type = 'saving'
GROUP BY b.id,b.address,a.type;