MySQL计算错误的总和

MySQL计算错误的总和,mysql,sum,Mysql,Sum,我正在努力计算MySQL Workbench中另一个计算的总和。我真的不知道如何用语言来解释,所以我会提供一些数据 这里有一些表格和数据: drop database if exists GDPR; create database if not exists GDPR; use GDPR; drop table if exists Company; create table Company ( id_company int not null auto_increment,

我正在努力计算MySQL Workbench中另一个计算的总和。我真的不知道如何用语言来解释,所以我会提供一些数据

这里有一些表格和数据:

drop database if exists GDPR;
create database if not exists GDPR;

use GDPR;

drop table if exists Company;

create table Company 
(
    id_company int not null auto_increment,
    name varchar (50),
    primary key (id_company)
) auto_increment = 1;

drop table if exists GDPR_steps;

create table GDPR_steps 
(
    id_step int not null auto_increment,
    id_company int,
    name varchar (50),
    primary key (id_step),
    foreign key (id_company) references Company (id_company)
) auto_increment = 1;

drop table if exists compliance;

create table compliance  
(
    id_com int not null auto_increment,
    id_step int,
    initiative varchar (50),
    status varchar (10),
    primary key (id_com),
    foreign key (id_step) references gdpr_steps (id_step)
) auto_increment = 1;

insert into company 
values (null, 'Mango'), (null, 'Kiwi');

insert into gdpr_steps 
values (null, 1, 'Awareness'), (null, 1, 'Information you hold'),
       (null, 2, 'Awareness'), (null, 2, 'Information you hold');

insert into compliance 
values (null, 1, 'E-mail all employees',  '1'),
       (null, 1, 'E-mail all applicants', '0'),
       (null, 2, 'Delete some data', '1'),
       (null, 3, 'Call stakeholders', '1'),
       (null, 4, 'Review data', '0');
我有一个查询,它根据属于某个步骤的每个
计划的
状态
,计算每个公司每个步骤的完成率

select 
    company.name as 'Company',
    gdpr_steps.name as 'ID Step',
    (sum(compliance.status)/count(compliance.status)) * 100 as 'Ratio %'
from
    compliance, company, gdpr_steps
where 
    gdpr_steps.id_step = compliance.id_step 
    and company.id_company = gdpr_steps.id_company
group by 
    compliance.id_step, company.id_company;
上面的查询返回此输出:

Company   ID Step               Ratio %
-----------------------------------------
Mango     Awareness             50
Mango     Information you hold  100
Kiwi      Awareness             100
Kiwi      Information you hold  0
现在,当我想计算每家公司的比率时(例如,将步骤1中的比率和步骤2中的比率相加,然后除以2),我无法让它工作。那会是什么样子

Company   Overall ratio %
Mango     (Awareness (50) + Information you hold (100)) / nr of steps (2 in our case)
Kiwi      (Awareness  (0) + Information you hold (100)) / nr of steps (2 in our case)
在我们的案例中,这将导致如下结果:

Name    Overall ratio %
Mango   75
Kiwi    50
我试过类似的东西

select 
    company.name,
    ((sum(compliance.status)/count(compliance.status)) * 100)/ count(gdpr_steps.id_step)
from
    compliance, company, gdpr_steps 
where 
    gdpr_steps.id_step = compliance.id_step 
    and company.id_company = gdpr_steps.id_company
group by 
    company.id_company;
这一个似乎根本不起作用,因为我收到的值与预期值完全不同

你能解释一下我做错了什么吗


亲切的问候

看来你在追求以下目标

SELECT company
     , AVG(`ratio %`) n
  FROM
     (
        -- your query here --
     ) x
 GROUP
    BY company;

请注意,大约在1992年,我们停止了以这种方式编写查询。为什么不遵守?另外,你提供了一些样本数据,这很好,但是你能在发布之前测试数据吗?很抱歉,这是我在学校学到的。。。到目前为止,我有3-4个MySQL类。我为我的写作感到抱歉。我已经上传了问题,我已经修复了错误;现在应该可以了。如果他们仍然教授旧的逗号连接语法而不是内部连接语法,那就去其他学校吧。他们已经编辑了我的问题,现在它更好地解释了我要做的事情。我也尝试了你的代码,但它没有返回我期望收到的值。如果你愿意多帮我一点,请你看看更新的问题,也许现在更容易理解我需要什么。是的!就是那个!好的,非常感谢你给我的第一个。我已经看到了编写查询的方法,我将尝试适应。我还有一个问题:您使用的
n
x
是用来定义。。。?我的意思是,我知道它们是必需的,但我不太明白它们有什么好处。它们是别名,分别是列别名和表别名。列别名非常简单,因此很容易在应用程序代码中引用结果。表别名是必需的,但对于引用也很有用,特别是在更复杂的查询中。