SQL JOINS和GROUPING

时间:2017-08-21 06:27:38

标签: sql oracle oracle11g

下表是需要连接以准备所需输出的输入表。

帐户:

ACCTNO          BRANCHCODE
501000524165    100
501000524220    101
501000524275    102
501000524330    103
501000524385    104
501000524440    105
501000524495    106
501000524550    107
501000524605    108
501000524660    109
501000524715    100
501000524770    101

TRANSACTION:

Trans_id    Trans_Amt   AcctNo
1           2500        501000524165
2           5000        501000524220
3           7500        501000524275
4           10000       501000524330
5           12500       501000524385
6           15000       501000524440

分支:

BRANCH_CODE BRANCH_NAME BRANCH_PHONE
100         BANGALORE   1234567890
101         CHENNAI     2345678901
102         HYDERABAD   345678901
103         TRIVENDRAM  4567890123
104         KOCHI       56789012
105         BHUBANES    6789012345
106         MUMBAI      7890123456
107         KOLKATA     890124567
108         MYSORE      901245678

我需要这样的输出

BRANCHNAME         BRANCHPHONE          TRANSACTION_AMT

1 个答案:

答案 0 :(得分:1)

在表格之间使用常规JOIN,您可以获得所需的输出。

SELECT  B.BRANCH_NAME,
        B.BRANCH_PHONE,
        T.TRANS_AMT
FROM    BRANCHES B
JOIN    ACCTS A
ON      B.BRANCH_CODE = A.BRANCH_CODE
JOIN    TRANSACTIONS T
ON      A.ACCT_NO = T.ACCT_NO

输出

BRANCH_NAME  BRANCH_PHONE  TRANS_AMT  
-----------  ------------  -----------
BANGALORE    1234567890    2500
CHENNAI      2345678901    5000
HYDERABAD    345678901     7500
TRIVENDRAM   4567890123    10000
KOCHI        56789012      12500
BHUBANES     6789012345    15000