简体   繁体   中英

calculate different values from mysql to php tables

How can I compile the results of various calculations from data gathered from different tables ? Present summaries in php table. I have people in a table that is linked to two other tables. It will be sorted by department how many people are women and men in number and percentage . How much they earn in total per department in average. How big women's pay is a percentage of men and how great the wage distribution is the max and min values ​​for men and women.

These code is used to get all the data.

I have added some value manually just for clarification in this picture . I have struggled with this for some time now and see no solution. Grateful for the help !

Please see below, it's not exactly as you need, since I don't know a structure of tables, but you will get an idea:

CREATE TABLE `usergroup` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `sex` enum('yes','no') NOT NULL DEFAULT 'yes',
  `salary` decimal(10,2) DEFAULT NULL,
  `userGroupId` int(11) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;


insert into `usergroup`(`id`,`name`) values (1,'Sales');
insert into `usergroup`(`id`,`name`) values (2,'Support');
insert into `usergroup`(`id`,`name`) values (3,'Managment');
insert into `usergroup`(`id`,`name`) values (4,'Others');

insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (1,'yes',20000.00,1,'Scott');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (2,'yes',30000.00,1,'Peter');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (3,'no',20000.00,1,'Mike');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (4,'yes',100000.00,2,'Senior');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (5,'no',50000.00,2,'Junior');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (6,'yes',75000.00,2,'Middle');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (7,'yes',250000.00,3,'King');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (8,'yes',300000.00,3,'Ace');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (9,'no',200000.00,3,'Queen');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (10,'yes',100000.00,3,'Jack');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (11,'no',400000.00,3,'LadyJoker');

Query to get report. The subquery is selecting a data with stats values, which are used in the outer query for calculation:

select analytic.userGroupId, 
       analytic.name, 
       analytic.countMen, 
       analytic.countWomen, 
       round((analytic.countMen / analytic.countMisc)*100, 2) as percentMen,
       round((analytic.countWomen / analytic.countMisc)*100, 2) as percentWomen,
       round((analytic.maxSalaryMen + analytic.minSalaryMen)/2, 2) as basicSalaryMen,  
       round((analytic.maxSalaryWomen + analytic.minSalaryWomen)/2, 2) as basicSalaryWomen,  
       round(
                (analytic.maxSalaryMen + analytic.minSalaryMen +
                 analytic.maxSalaryWomen + analytic.minSalaryWomen
                )/4,
            2)  
       as basicSalaryMisc,  
       if(analytic.maxSalaryMen + analytic.minSalaryMen = 0, 100,
           round((analytic.maxSalaryWomen + analytic.minSalaryWomen)/(analytic.maxSalaryMen + analytic.minSalaryMen), 2)
       ) as salaryBasicWomenPercentOfMen,  
       analytic.maxSalaryMen, 
       analytic.minSalaryMen, 
       analytic.maxSalaryWomen, 
       analytic.minSalaryWomen
from (
  select ug.id as userGroupId,
         ug.name,
         sum(if(u.sex='yes', 1, 0)) countMen,
         sum(if(u.sex='no', 1, 0)) countWomen,
         count(*) countMisc,
         max(if(u.sex='yes', u.salary, null)) maxSalaryMen,
         min(if(u.sex='yes', u.salary, null)) minSalaryMen,
         max(if(u.sex='no', u.salary, null)) maxSalaryWomen,
         min(if(u.sex='no', u.salary, null)) minSalaryWomen
  from userGroup ug left join user u 
  on u.userGroupId = ug.id 
  group by ug.id, ug.name
  order by ug.name
) analytic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM