Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- create table People(ID integer, Name varchar(100),Address varchar(100));
- create table Donations(ID integer, People_ID integer,Donation_Date varchar(100),Party integer);
- insert into People(ID, Name, Address) values(1, "Adam", "95070");
- insert into Donations(ID, People_ID, Donation_Date, Party) values(99, 1, "3/25/2020",-1);
- insert into Donations(ID, People_ID, Donation_Date, Party) values(96, 1, "3/25/2021",-1);
- insert into Donations(ID, People_ID, Donation_Date, Party) values(95, 1, "1/25/2020",-1);
- insert into People(ID, Name, Address) values(2, "Kellie", "95080");
- insert into Donations(ID, People_ID, Donation_Date, Party) values(98, 2, "3/23/2020",-1);
- insert into People(ID, Name, Address) values(3, "Alex", "95070");
- DELIMITER $$
- CREATE FUNCTION `numberToParty`(n INT) RETURNS varchar(100)
- BEGIN
- declare ans varchar(100);
- set ans = '';
- case
- when n>=1 then set ans='R';
- when n<=-1 then set ans='D';
- when n=0 then set ans='U';
- else set ans='U';
- end case;
- return trim(ans);
- END$$
- DELIMITER ;
- Select People.name ,IFNULL(max(Donations.Donation_Date),''),numberToParty(sum(Donations.Party)), People.Address from People left join Donations on People.id=Donations.People_ID GROUP BY People.ID, People.name, People.Address;
- https://paiza.io/projects/YXd-O-OerrkYt43ILrDgQQ?language=mysql
Add Comment
Please, Sign In to add comment