Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE test.Names (
- id INT NOT NULL ,
- salary INT NULL ,
- name VARCHAR(45) NULL ,
- tmpl INT,
- PRIMARY KEY (`id`) ,
- UNIQUE INDEX `id_UNIQUE` (`id` ASC)
- );
- id, salary, name, tmpl
- 1, 10, John, null
- 2, 20, Pat, null
- 3, 30, Jane, null
- salary of each person/Sum(salary)*100
- id, salary, name, tmpl
- 1, 10, John, 16.67
- 2, 20, Pat, 33.33
- 3, 30, Jane, 50
- SET @sum_salaries = (SELECT SUM(salary) FROM test.Names);
- UPDATE test.Names
- SET tmpl = salary/@sum_salaries * 100;
- SET @SallarySum = (SELECT SUM(salary) FROM TableName);
- UPDATE Names t1
- INNER JOIN
- (
- SELECT id, (Salary / @SallarySum) * 100 tmpl
- FROM Names
- ) t2 ON t1.Id = t2.Id
- SET t1.tmpl = t2.tmpl;
- update `test`.`Names`
- set tmpl = salary / (select sum(salary) from `test`.`Names`) * 100
- where tmpl is null --for multiple times execution
Add Comment
Please, Sign In to add comment