Guest User

Untitled

a guest
Jan 17th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. CREATE TABLE test.Names (
  2. id INT NOT NULL ,
  3. salary INT NULL ,
  4. name VARCHAR(45) NULL ,
  5. tmpl INT,
  6. PRIMARY KEY (`id`) ,
  7. UNIQUE INDEX `id_UNIQUE` (`id` ASC)
  8. );
  9.  
  10. id, salary, name, tmpl
  11. 1, 10, John, null
  12. 2, 20, Pat, null
  13. 3, 30, Jane, null
  14.  
  15. salary of each person/Sum(salary)*100
  16.  
  17. id, salary, name, tmpl
  18. 1, 10, John, 16.67
  19. 2, 20, Pat, 33.33
  20. 3, 30, Jane, 50
  21.  
  22. SET @sum_salaries = (SELECT SUM(salary) FROM test.Names);
  23.  
  24. UPDATE test.Names
  25. SET tmpl = salary/@sum_salaries * 100;
  26.  
  27. SET @SallarySum = (SELECT SUM(salary) FROM TableName);
  28.  
  29. UPDATE Names t1
  30. INNER JOIN
  31. (
  32. SELECT id, (Salary / @SallarySum) * 100 tmpl
  33. FROM Names
  34. ) t2 ON t1.Id = t2.Id
  35. SET t1.tmpl = t2.tmpl;
  36.  
  37. update `test`.`Names`
  38. set tmpl = salary / (select sum(salary) from `test`.`Names`) * 100
  39. where tmpl is null --for multiple times execution
Add Comment
Please, Sign In to add comment