我必须创建一个函数,在该函数中,我将为每个客户找到他租金最高的年份和月份。 至于现在,我已经完成了:
Select CUSTOMER_ID, NAME, SURNAME, count(distinct RENT_ID_PK) as total
from t_rent
inner join t_customer on t_customer.CUSTOMER_ID_PK=t_rent.CUSTOMER_ID
group by CUSTOMER_ID
order by total desc;
说实话,不知道如何前进。 有提示吗?
@edit
CREATE TABLE `database`.`t_rent` (
`RENT_ID_PK` INT(10) NOT NULL AUTO_INCREMENT,
`CAR_ID` INT(10) NOT NULL,
`CUSTOMER_ID` INT(10) NOT NULL,
`STARTING_DATE` DATE NOT NULL,
`FINISH_DATE` DATE NOT NULL,
`COMPANY_ID_START` INT NOT NULL,
`COMPANY_ID_FINISH` INT NOT NULL,
`RENT_COST` INT UNSIGNED NOT NULL,
PRIMARY KEY (`RENT_ID_PK`));
CREATE TABLE `database`.`t_customer` (
`CUSTOMER_ID_PK` INT(10) NOT NULL AUTO_INCREMENT,
`NAME` VARCHAR(45) NOT NULL,
`SURNAME` VARCHAR(45) NOT NULL,
`E_MAIL` VARCHAR(45) NOT NULL,
`PHONE_NO` VARCHAR(45) NOT NULL,
`PERSONAL_NO` VARCHAR(45) NOT NULL,
`CREDIT_CARD_NO` VARCHAR(45) NOT NULL,
`ADRESS` INT(10) NOT NULL,
PRIMARY KEY (`CUSTOMER_ID_PK`));