0

如何根据从不同表格收集的数据编译各种计算的结果?在 php 表中显示摘要。我在一个链接到另外两个表的表中有人员。它将按部门的数量和百分比对女性和男性的人数进行排序。他们平均每个部门的总收入。女性的工资占男性的百分比有多大,工资分布的最大值和最小值是男性和女性的最大值。

这些代码用于获取所有数据。

我手动添加了一些值,只是为了在这张图片中进行澄清。我已经为此苦苦挣扎了一段时间,但没有找到解决方案。感谢您的帮助!

4

1 回答 1

0

请看下面,它并不完全符合您的需要,因为我不知道表格的结构,但您会有所了解:

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');

查询以获取报告。子查询正在选择具有统计值的数据,这些值在外部查询中用于计算:

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
于 2013-11-11T18:31:11.180 回答