MIN / MAX VALUES GROUP BY ID

时间:2017-10-20 09:49:19

标签: sql oracle group-by

查找余额最高的客户和每个分行贷款最低的客户的姓名和地址。

表格

CREATE TABLE Branch(
BranchID INT NOT NULL,
           Address varchar(100),
           ManagerSsn varchar(100),
           Branch_name varchar(255),
    PRIMARY KEY (BranchID)
);


CREATE TABLE Has_Account(
Assn Int,
           ANo CHAR(16),
           Opened_date date,
    PRIMARY KEY(Assn),
FOREIGN KEY(Assn) REFERENCES Customer(Ssn),
FOREIGN KEY(ANo) REFERENCES Account(AccountNo)
);

CREATE TABLE Account(
           AccountNo CHAR(16) NOT NULL,
           BranchID Int,
           Balance Int,
    PRIMARY KEY(AccountNo),
    FOREIGN KEY(BranchID) REFERENCES Branch(BranchID)
);

CREATE TABLE Customer(
Ssn Int CHECK(0101000000000 < Ssn < 3112999995999) NOT NULL,
           Address VARCHAR(100),
           Name VARCHAR(100),
           EmployerID Int,
    PRIMARY KEY(Ssn) 
);

CREATE TABLE Loan(
LoanNo CHAR(16) NOT NULL,
BranchID Int,
           Amount Int,
    PRIMARY KEY(LoanNo),
    FOREIGN KEY(BranchID) REFERENCES Branch(BranchID)
);

CREATE TABLE Has_Loan(
LNo CHAR(16),
Lssn Int,
Repay_date date,
PRIMARY KEY (LNo),
FOREIGN KEY(Lno) REFERENCES Loan(LoanNo),
FOREIGN KEY(Lssn) REFERENCES Customer(Ssn)
);

填充表格

INSERT INTO CUSTOMER
VALUES('2004980890999', ‘Kanaribakken 2B’, ‘Paul Stolten’, ‘’);

INSERT INTO CUSTOMER
VALUES(‘1010968250234’, ‘Parkveien 5’, ‘Anders Nilsson’, ‘’, );

INSERT INTO CUSTOMER
VALUES(‘2104996495987’, ‘Kongeveien 24B’, ‘Karoline Torskerud’, ‘123’, );

INSERT INTO CUSTOMER
VALUES(‘2910943135444’, ‘Gåsesmauet 3’, ‘Bente Arildsen’, ‘555’, );

INSERT INTO CUSTOMER
VALUES(‘1304980890678’, ‘Knallebakken 30’, ‘Knut Solberg’, ‘555’,);

INSERT INTO CUSTOMER
VALUES('1019730000234', ‘Laksebakken 40’, ‘Harald Olsen’, ‘’);

INSERT INTO BRANCH
VALUES (‘123’, ‘Børsveien 49’, ‘1412963670786’, ‘Nesttun’,);

INSERT INTO BRANCH
VALUES(‘555’, ‘Vimpelveien 24’, ‘2412983905999’, ‘DNB’,);

INSERT INTO ACCOUNT
VALUES (‘1234567890087620’, ‘123’, ‘459’,);

INSERT INTO ACCOUNT
VALUES (‘8402849284910340’, ‘123’, ‘35120’,);

INSERT INTO ACCOUNT
VALUES (‘3452948293817280’, ‘555’, ‘69’,);

INSERT INTO HAS_ACCOUNT
VALUES(‘1010968250234’, ‘1234567890087620’, ‘17.04.2012’,);

INSERT INTO HAS_ACCOUNT
VALUES (‘2104996495987’, ‘8402849284910340’, ‘20.09.2000’,);

INSERT INTO HAS_ACCOUNT
VALUES (‘2910943135444’, ‘3452948293817280’, ‘29.01.2006’,);

INSERT INTO LOAN
VALUES (‘2291928394039280’, ‘555’, ‘50000’,);

INSERT INTO LOAN 
VALUES (‘1295829384932100’, ‘123’, ‘400000’,);

INSERT INTO LOAN 
VALUES ('1295829384932222', ‘123’, ‘40000’);

INSERT INTO HAS_LOAN
VALUES (‘2910943135444’, ‘2291928394039280’, ‘09.09.2019’,);

INSERT INTO HAS_LOAN
VALUES (‘2104996495987’, ‘1295829384932100’, ‘14.03.2022’,);

INSERT INTO HAS_LOAN
VALUES ('2004980890999', '1295829384932222', '01.02.2021');
到目前为止

草稿:

SELECT DISTINCT Customer.Name, Customer.Address
FROM Customer, Account, Loan, Has_Account, Has_Loan
WHERE Customer.Ssn = Has_Account.Assn AND Has_Account.Assn = Has_Loan.Lssn 
    (SELECT Account.Balance MAX(Balance) GROUP BY(BranchID)) AND 
    Loan.AMOUNT(SELECT Loan.Amount MIN(Amount) GROUP BY(BranchID));

我认为我们需要通过BranchID对输出进行分组是正确的,并且max / min是可以的。看起来声明手续在某种程度上是错误的。 非常感谢任何帮助或暗示!

0 个答案:

没有答案