2

So i have this assingment in school, were i am supposed to make a tool for a DPO in a firm. The assignment is as follows: "You are required to develop an app or on-line tool where you can enter the status of the many details that the regulations states for the company. The intended user of the tool is a Data Protection Officer (DPO) or an employee responsible for data projection in general. The tool shall be able to handle input from more than one company and more than one audit for the individual company. The tool shall be able to calculate the level of compliance to the GDPR." So as for the calculator i thought about a solution, where the DPO for a given company, can give one of the 12 regulations either a 1 or 0, where 1 is that the regulation is complied and 0 is not. So i wanted to take the sum and then divide with 12, to get the compliance average number. But how do i do this in SQL? As of now, i have already made the comany, auditor and regulation table, which looks like this:

    CREATE TABLE IF NOT EXISTS`Companies` (
    `idCompanies` INT NOT NULL,
    `cvr_nr` INT NULL,
    `Dato` DATE NULL,
    PRIMARY KEY (`idCompanies`))
    ENGINE = InnoDB;

    CREATE TABLE IF NOT EXISTS `Auditors` (
      `idAuditors` INT NOT NULL,
      `Auditor_name` VARCHAR(45) NULL,
      PRIMARY KEY (`idAuditors`))
    ENGINE = InnoDB;

insert into auditors values (1, "Lars Larsen");
insert into auditors values (2, "Henrik Andersen");
insert into auditors values (3, "Jens Andersen");

drop table if exists regulations;
CREATE TABLE IF NOT EXISTS `Regulations` (
  `idRegulations` INT NOT NULL auto_increment,
  `Regulation_name` VARCHAR(100) NULL,
  `Regulation_details` VARCHAR(400) NULL,
  PRIMARY KEY (`idRegulations`))
ENGINE = InnoDB;

    insert into regulations values (null, "xx", "xxx");
    insert into regulations values (null, "xx", "xxx");

#The XX are just as examples.

CREATE TABLE IF NOT EXISTS `Companies_has_Regulations` (
  `Companies_idCompanies` INT NOT NULL,
  `Regulations_idRegulations` INT NOT NULL,
  PRIMARY KEY (`Companies_idCompanies`, `Regulations_idRegulations`),
  INDEX `fk_Companies_has_Regulations_Regulations1_idx` (`Regulations_idRegulations` ASC),
  INDEX `fk_Companies_has_Regulations_Companies1_idx` (`Companies_idCompanies` ASC),
  CONSTRAINT `fk_Companies_has_Regulations_Companies1`
    FOREIGN KEY (`Companies_idCompanies`)
    REFERENCES `Companies` (`idCompanies`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Companies_has_Regulations_Regulations1`
    FOREIGN KEY (`Regulations_idRegulations`)
    REFERENCES `Regulations` (`idRegulations`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;
Jake Long
  • 25
  • 6

1 Answers1

0

Select all rows from Regulations, LEFT JOIN Companies_has_Regulations ON the regulation id and filter the company in question. Then you got all regulations and for each row an entry in Companies_has_Regulations existed the values from Companies_has_Regulations in additional columns. For a row from Regulations where no entry existed in Companies_has_Regulations these columns have null values. Now you can use count() over the ID from Regulations to get the count of all regulations and count() over Regulations_idRegulations to get the count of all regulations the company complies with (count(column) doesn't count rows where column IS NULL). Put that in a division and you got your rate of compliance.

SELECT count(chr.Regulations_idRegulations)
       /
       count(r.idRegulations) level_of_compliance
       FROM Regulations r
            LEFT JOIN Companies_has_Regulations chr
                      ON chr.Regulations_idRegulations = r.idRegulations
       WHERE chr.Companies_idCompanies = <ID for the company in question>;

Replace <ID for the company in question> with the respective company ID.

sticky bit
  • 36,626
  • 12
  • 31
  • 42
  • Thank you very much for your answer. Maybe this is obvious, but i am a bit confused, of what "chr." stands for? – Jake Long Jun 05 '18 at 13:26
  • @JakeLong: `chr` is just an [alias](https://www.w3schools.com/sql/sql_alias.asp) for `Companies_has_Regulations`. Aliases can be introduced for a table by adding it after a space after the table name in `FROM` or any `JOIN`. Optionally one can put the keyword `AS` between the table name and the alias. – sticky bit Jun 05 '18 at 15:58