Changes between Initial Version and Version 1 of AdvancedReport17


Ignore:
Timestamp:
08/21/26 06:53:54 (28 hours ago)
Author:
235018
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • AdvancedReport17

    v1 v1  
     1= Number of product changes each employee has made in the last month
     2{{{#!sql
     3CREATE OR REPLACE FUNCTION get_employee_product_changes_last_month()
     4RETURNS TABLE (
     5    ssn BIGINT,
     6    employee_name TEXT,
     7    number_of_product_changes BIGINT
     8)
     9LANGUAGE plpgsql
     10AS $$
     11BEGIN
     12    RETURN QUERY
     13    SELECT
     14        e.ssn,
     15        p.name AS employee_name,
     16        COUNT(DISTINCT mc.date_and_time) AS number_of_product_changes
     17    FROM employees e
     18    JOIN personal p
     19        ON e.ssn = p.ssn
     20    LEFT JOIN makes_change mc
     21        ON e.ssn = mc.ssn
     22    LEFT JOIN change ch
     23        ON mc.date_and_time = ch.date_and_time
     24    WHERE
     25        mc.date_and_time >= date_trunc('month', CURRENT_DATE) - INTERVAL '1 month'
     26        AND mc.date_and_time < date_trunc('month', CURRENT_DATE)
     27    GROUP BY
     28        e.ssn,
     29        p.name
     30    ORDER BY
     31        number_of_product_changes DESC;
     32END;
     33$$;
     34
     35}}}
     36
     37== Relational Algebra
     38- E(id, date_of_hire)
     39- P(id, name, first_name, last_name, email, password, permissions, type, authorisation)
     40- MC(id, date_and_time, permission, type, authorisation)
     41- CH(date_and_time, product_code, changes_made)
     42
     43**JOIN employees with their personal information:**
     44- J1 ← E ⨝E.SSN = P.SSN P
     45
     46**JOIN employees with the changes they have made:**
     47- J2 ← J1 ⨝E.SSN = MC.SSN MC
     48
     49**JOIN the changes with the corresponding product:**
     50- J3 ← J2 ⨝MC.date_and_time = CH.date_and_time CH
     51
     52**SELECT only changes made during the last calendar month:**
     53- F1 ← σ
     54date_and_time ≥ START_OF_CURRENT_MONTH - 1 MONTH
     55
     56date_and_time < START_OF_CURRENT_MONTH
     57(J3)
     58
     59**Calculate total number of distinct changes made by each employee:**
     60- R ← γSSN, name;
     61     COUNT(DISTINCT date_and_time) → number_of_product_changes
     62     (F1)
     63
     64**Sort by total number of changes made:**
     65- R_final ← τnumber_of_product_changes DESC(R)
     66
     67