Changes between Initial Version and Version 1 of AdvancedReport13


Ignore:
Timestamp:
08/21/26 06:33:05 (29 hours ago)
Author:
235018
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • AdvancedReport13

    v1 v1  
     1= Each store's number of request, including how many have been solved, and how many are still in progress
     2
     3{{{#!sql
     4CREATE OR REPLACE FUNCTION get_store_request_statistics()
     5RETURNS TABLE (
     6    store_id INT,
     7    store_name TEXT,
     8    total_requests BIGINT,
     9    solved_requests BIGINT,
     10    requests_in_progress BIGINT
     11)
     12LANGUAGE plpgsql
     13AS $$
     14BEGIN
     15    RETURN QUERY
     16    SELECT
     17        s.store_id,
     18        s.name AS store_name,
     19        COUNT(r.request_num) AS total_requests,
     20        COUNT(
     21            CASE
     22                WHEN r.customer_satisfaction IS NOT NULL
     23                THEN 1
     24            END
     25        ) AS solved_requests,
     26        COUNT(
     27            CASE
     28                WHEN r.customer_satisfaction IS NULL
     29                THEN 1
     30            END
     31        ) AS requests_in_progress
     32    FROM store s
     33    LEFT JOIN for_store fs
     34        ON s.store_id = fs.store_id
     35    LEFT JOIN request r
     36        ON fs.request_num = r.request_num
     37    GROUP BY
     38        s.store_id,
     39        s.name
     40    ORDER BY
     41        total_requests DESC;
     42END;
     43$$;
     44
     45}}}
     46
     47== Relational Algebra
     48- S(store_id, name, date_of_founding, physical_address, store_email, rating)
     49- FS(request_num, store_id)
     50- R(request_num, date_and_time, problem, notes_of_communication, customer_satisfaction)
     51
     52**JOIN stores with their requests:**
     53- J1 ← S ⟕S.store_id = FS.store_id FS
     54- J2 ← J1 ⟕FS.request_num = R.request_num R
     55
     56**Calculate total number of requests for each store:**
     57- total_requests = COUNT(request_num)
     58
     59**Calculate solved and unsolved requests:**
     60- solved_requests = COUNT(request_num) WHERE customer_satisfaction IS NOT NULL
     61- requests_in_progress = COUNT(request_num) WHERE customer_satisfaction IS NULL
     62- R1 ← γstore_id, name;
     63     COUNT(request_num) → total_requests,
     64     COUNT(customer_satisfaction) → solved_requests,
     65     COUNT(request_num) - COUNT(customer_satisfaction)
     66         → requests_in_progress
     67     (J2)
     68
     69**Sort by total number of requests:**
     70- R_final ← τtotal_requests DESC(R1)
     71
     72