whitesurge

Mysql tester

Mar 26th, 2020
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. create table People(ID integer, Name varchar(100),Address varchar(100));
  2. create table Donations(ID integer, People_ID integer,Donation_Date varchar(100),Party integer);
  3.  
  4. insert into People(ID, Name, Address) values(1, "Adam", "95070");
  5. insert into Donations(ID, People_ID, Donation_Date, Party) values(99, 1, "3/25/2020",-1);
  6. insert into Donations(ID, People_ID, Donation_Date, Party) values(96, 1, "3/25/2021",-1);
  7. insert into Donations(ID, People_ID, Donation_Date, Party) values(95, 1, "1/25/2020",-1);
  8.  
  9. insert into People(ID, Name, Address) values(2, "Kellie", "95080");
  10. insert into Donations(ID, People_ID, Donation_Date, Party) values(98, 2, "3/23/2020",-1);
  11.  
  12. insert into People(ID, Name, Address) values(3, "Alex", "95070");
  13.  
  14.  
  15.  
  16. DELIMITER $$
  17. CREATE FUNCTION `numberToParty`(n INT) RETURNS varchar(100)
  18. BEGIN
  19.  
  20. declare ans varchar(100);
  21.  
  22.  
  23. set ans = '';
  24.  
  25. case
  26. when n>=1 then set ans='R';
  27. when n<=-1 then set ans='D';
  28. when n=0 then set ans='U';
  29. else set ans='U';
  30. end case;
  31.  
  32.  
  33.  
  34. return trim(ans);
  35. END$$
  36.  
  37. DELIMITER ;
  38.  
  39.  
  40. 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;
  41.  
  42.  
  43. https://paiza.io/projects/YXd-O-OerrkYt43ILrDgQQ?language=mysql
Add Comment
Please, Sign In to add comment