Changes between Version 1 and Version 2 of Views


Ignore:
Timestamp:
09/15/25 04:11:35 (5 days ago)
Author:
222004
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Views

    v1 v2  
    1414
    1515CREATE OR REPLACE VIEW synergymed.v_client_full_activity_overview AS
     16WITH order_summary AS (
     17    SELECT co.client_id,
     18           COUNT(co.id) AS total_orders,
     19           COALESCE(SUM(co.total_price), 0) AS total_spent
     20    FROM synergymed.clientorder co
     21    GROUP BY co.client_id
     22),
     23payment_summary AS (
     24    SELECT p.client_id,
     25           COUNT(*) FILTER (WHERE p.status = 'завршено') AS successful_payments,
     26           COUNT(*) FILTER (WHERE p.status = 'неуспешно') AS failed_payments,
     27           MAX(p.payment_date) AS last_payment_date
     28    FROM synergymed.payment p
     29    GROUP BY p.client_id
     30),
     31allergy_clients AS (
     32    SELECT DISTINCT hp.client_id
     33    FROM synergymed.healthprofile hp
     34    JOIN synergymed.allergicreaction_healthprofile_medicine ahm
     35         ON ahm.health_profile_id = hp.id
     36),
     37prescription_clients AS (
     38    SELECT DISTINCT pr.client_id
     39    FROM synergymed.prescription pr
     40    WHERE pr.valid_to >= CURRENT_DATE
     41)
    1642SELECT
    1743    u.id AS user_id,
     
    2046    c.is_verified,
    2147    COALESCE(cc.points, 0) AS loyalty_points,
    22    
    23     -- narachki
    24     COUNT(DISTINCT co.id) AS total_orders,
    25     COALESCE(SUM(co.total_price), 0) AS total_spent,
    26    
    27     -- plakjanja
    28     COUNT(DISTINCT p.id) FILTER (WHERE p.status = 'завршено') AS successful_payments,
    29     COUNT(DISTINCT p.id) FILTER (WHERE p.status = 'неуспешно') AS failed_payments,
    30     MAX(p.payment_date) AS last_payment_date,
    31    
    32     -- alergii
    33     CASE
    34         WHEN EXISTS (
    35             SELECT 1 FROM synergymed.healthprofile hp
    36             JOIN synergymed.allergicreaction_healthprofile_medicine ahm ON ahm.health_profile_id = hp.id
    37             WHERE hp.client_id = c.user_id
    38         ) THEN TRUE ELSE FALSE
    39     END AS has_allergies,
    40    
    41     -- recepti
    42     CASE
    43         WHEN EXISTS (
    44             SELECT 1 FROM synergymed.prescription pr
    45             WHERE pr.client_id = c.user_id
    46               AND pr.valid_to >= CURRENT_DATE
    47         ) THEN TRUE ELSE FALSE
    48     END AS has_active_prescriptions
     48
     49    COALESCE(order_summary.total_orders, 0) AS total_orders,
     50    COALESCE(order_summary.total_spent, 0) AS total_spent,
     51
     52    COALESCE(payment_summary.successful_payments, 0) AS successful_payments,
     53    COALESCE(payment_summary.failed_payments, 0) AS failed_payments,
     54    payment_summary.last_payment_date,
     55
     56    (allergy_clients.client_id IS NOT NULL) AS has_allergies,
     57    (prescription_clients.client_id IS NOT NULL) AS has_active_prescriptions
    4958
    5059FROM synergymed.client c
    5160JOIN synergymed.users u ON u.id = c.user_id
    5261LEFT JOIN synergymed.clubcard cc ON cc.user_id = c.user_id
    53 LEFT JOIN synergymed.clientorder co ON co.client_id = c.user_id
    54 LEFT JOIN synergymed.payment p ON p.client_id = c.user_id
    55 GROUP BY u.id, client_name, u.e_mail, c.is_verified, cc.points;
     62LEFT JOIN order_summary ON order_summary.client_id = c.user_id
     63LEFT JOIN payment_summary ON payment_summary.client_id = c.user_id
     64LEFT JOIN allergy_clients ON allergy_clients.client_id = c.user_id
     65LEFT JOIN prescription_clients ON prescription_clients.client_id = c.user_id;
    5666
    5767}}}