Changes between Version 10 and Version 11 of AdvancedReports


Ignore:
Timestamp:
06/02/26 21:53:23 (9 days ago)
Author:
232012
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • AdvancedReports

    v10 v11  
    1010
    1111{{{
     12SET search_path TO project;
     13
    1214WITH product_sales_yearly AS (
    1315    SELECT
    1416        op.product_id,
    1517        SUM(op.quantity) AS total_sold_yearly,
    16 
    1718        SUM(op.quantity) / 365.0 AS daily_sales_velocity
    18     FROM project.ORDER_PRODUCTS op
    19     JOIN project.ORDERS o ON op.order_id = o.order_id
     19
     20    FROM ORDER_PRODUCTS op
     21    JOIN ORDERS o ON op.order_id = o.order_id
    2022
    2123    WHERE o.purchase_date >= CURRENT_DATE - INTERVAL '1 year'
     
    3739            ELSE 9999
    3840        END AS days_until_out_of_stock
    39     FROM project.PRODUCTS p
    40     JOIN project.RELEASES r ON p.release_id = r.release_id
     41
     42    FROM PRODUCTS p
     43    JOIN RELEASES r ON p.release_id = r.release_id
    4144    JOIN product_sales_yearly psy ON p.product_id = psy.product_id
    4245)
     
    4750    stock AS current_stock,
    4851    total_sold_yearly,
    49     ROUND(CAST(daily_sales_velocity AS NUMERIC), 4) AS daily_velocity,
     52
     53    ROUND(CAST(daily_sales_velocity AS NUMERIC), 2) AS daily_velocity,
    5054    ROUND(CAST(days_until_out_of_stock AS NUMERIC), 1) AS days_left,
    51 
    52     CEIL((daily_sales_velocity * 90) - stock) AS recommended_restock_quantity,
    53 
    54     ROUND((CEIL((daily_sales_velocity * 90) - stock) * price AS NUMERIC), 2) AS estimated_restock_cost
     55    CEIL((daily_sales_velocity * 90) - stock) AS recommended_restock_quantity
     56
    5557FROM inventory_velocity
    5658WHERE days_until_out_of_stock < 30
     
    6163
    6264{{{
    63 RELATIONAL ALGEBRA
     65ProductSalesYearly <-
     66γ product_id;
     67  total_sold_yearly := SUM(op.quantity),
     68  daily_sales_velocity := SUM(op.quantity) / 365.0
     69(
     70  σ o.purchase_date >= CURRENT_DATE - 1 year
     71    ∧ o.status ∈ {PAID, SHIPPED, DELIVERED}
     72  (
     73    ORDER_PRODUCTS op ⨝ (op.order_id = o.order_id) ORDERS o
     74  )
     75)
     76
     77InventoryVelocity <-
     78π product_id, format, stock, price, release_title,
     79  total_sold_yearly, daily_sales_velocity,
     80  days_until_out_of_stock :=
     81    CASE
     82      WHEN daily_sales_velocity > 0
     83        THEN stock / daily_sales_velocity
     84      ELSE 9999
     85    END
     86(
     87  (
     88    PRODUCTS p ⨝ (p.release_id = r.release_id) RELEASES r
     89  )
     90  ⨝ (p.product_id = psy.product_id) ProductSalesYearly psy
     91)
     92
     93Result <-
     94π product_id,
     95  release_title,
     96  format,
     97  current_stock := stock,
     98  total_sold_yearly,
     99  daily_velocity := ROUND(daily_sales_velocity, 4),
     100  days_left := ROUND(days_until_out_of_stock, 1),
     101  recommended_restock_quantity :=
     102    CEIL((daily_sales_velocity * 90) - stock)
     103(
     104  σ days_until_out_of_stock < 30
     105  (
     106    InventoryVelocity
     107  )
     108)
     109
     110τ daily_velocity ↓, days_left ↑ (Result)
    64111}}}
    65112
     
    73120
    74121{{{
     122SET search_path TO project;
     123
    75124WITH product_sales_6months AS (
    76125    SELECT
     
    78127        SUM(op.quantity) AS units_sold_6m,
    79128        MAX(o.purchase_date) AS last_purchase_date
    80     FROM project.ORDER_PRODUCTS op
    81     JOIN project.ORDERS o ON op.order_id = o.order_id
     129
     130    FROM ORDER_PRODUCTS op
     131    JOIN ORDERS o ON op.order_id = o.order_id
     132
    82133    WHERE o.purchase_date >= CURRENT_DATE - INTERVAL '6 months'
    83134      AND o.status IN ('PAID', 'SHIPPED', 'DELIVERED')
     
    88139        product_id,
    89140        COUNT(wishlist_id) AS wishlist_addition_count
    90     FROM project.WISHLIST_PRODUCTS
     141
     142    FROM WISHLIST_PRODUCTS
    91143    GROUP BY product_id
    92144)
     
    98150    p.price AS current_unit_price,
    99151    (p.stock * p.price) AS frozen_capital,
     152
    100153    COALESCE(TO_CHAR(ps.last_purchase_date, 'YYYY-MM-DD'), 'NEVER BOUGHT') AS last_sold_date,
    101154    COALESCE(pw.wishlist_addition_count, 0) AS times_on_wishlists,
     155
    102156    CASE
    103157        WHEN COALESCE(pw.wishlist_addition_count, 0) > 0 THEN 'Discount Target (Wishlisted)'
    104158        ELSE 'Deep Liquidation/Clearance Target'
    105159    END AS inventory_action_plan
    106 FROM project.PRODUCTS p
    107 JOIN project.RELEASES r ON p.release_id = r.release_id
     160
     161FROM PRODUCTS p
     162JOIN RELEASES r ON p.release_id = r.release_id
    108163LEFT JOIN product_sales_6months ps ON p.product_id = ps.product_id
    109164LEFT JOIN product_wishlist_counts pw ON p.product_id = pw.product_id
     165
    110166WHERE (ps.product_id IS NULL) AND (p.stock > 0)
    111167ORDER BY frozen_capital DESC, times_on_wishlists DESC;
     
    115171
    116172{{{
    117 RELATIONAL ALGEBRA
     173ProductSales6M <-
     174γ product_id;
     175  units_sold_6m := SUM(op.quantity),
     176  last_purchase_date := MAX(o.purchase_date)
     177(
     178  σ o.purchase_date >= NOW - 6 months
     179    ∧ o.status ∈ {PAID, SHIPPED, DELIVERED}
     180  (
     181    ORDER_PRODUCTS op ⨝ (op.order_id = o.order_id) ORDERS o
     182  )
     183)
     184
     185WishlistCounts <-
     186γ product_id;
     187  wishlist_addition_count := COUNT(wishlist_id)
     188(
     189  WISHLIST_PRODUCTS
     190)
     191
     192DeadStockAnalysis <-
     193π p.product_id,
     194  r.title,
     195  p.format,
     196  unsold_stock_quantity := p.stock,
     197  current_unit_price := p.price,
     198  frozen_capital := p.stock * p.price,
     199  last_sold_date := COALESCE(ps.last_purchase_date, 'NEVER BOUGHT'),
     200  times_on_wishlists := COALESCE(wc.wishlist_addition_count, 0),
     201  inventory_action_plan :=
     202    CASE
     203      WHEN COALESCE(wc.wishlist_addition_count, 0) > 0
     204        THEN 'Discount Target (Wishlisted)'
     205      ELSE 'Deep Liquidation/Clearance Target'
     206    END
     207(
     208  (
     209    PRODUCTS p ⨝ (p.release_id = r.release_id) RELEASES r
     210  )
     211  ⟕ ProductSales6M ps
     212  ⟕ WishlistCounts wc
     213)
     214
     215Result <-
     216σ ps.product_id IS NULL ∧ p.stock > 0
     217(
     218  DeadStockAnalysis
     219)
    118220}}}
    119221
     
    127229
    128230{{{
     231SET search_path TO project;
     232
    129233WITH discount_events AS (
    130234    SELECT
     
    134238        m.discount AS discount_percentage,
    135239        mp.product_id
    136     FROM project.MODIFICATIONS m
    137     JOIN project.MODIFICATION_PRODUCTS mp ON m.modification_id = mp.modification_id
     240
     241    FROM MODIFICATIONS m
     242    JOIN MODIFICATION_PRODUCTS mp ON m.modification_id = mp.modification_id
    138243    WHERE m.type_of_modification = 'DISCOUNT'
    139244),
     
    142247        de.modification_id,
    143248        de.product_id,
     249
    144250        COALESCE(SUM(op.quantity), 0) AS units_sold_before,
    145251        COALESCE(SUM(op.quantity * op.price_at_purchase), 0.00) AS revenue_before
     252
    146253    FROM discount_events de
    147     LEFT JOIN project.ORDER_PRODUCTS op ON de.product_id = op.product_id
    148     LEFT JOIN project.ORDERS o ON op.order_id = o.order_id
     254    LEFT JOIN ORDER_PRODUCTS op ON de.product_id = op.product_id
     255    LEFT JOIN ORDERS o ON op.order_id = o.order_id
    149256        AND o.purchase_date >= de.date_modified - INTERVAL '30 days'
    150257        AND o.purchase_date < de.date_modified
     
    158265        COALESCE(SUM(op.quantity), 0) AS units_sold_after,
    159266        COALESCE(SUM(op.quantity * op.price_at_purchase), 0.00) AS revenue_after
     267
    160268    FROM discount_events de
    161     LEFT JOIN project.ORDER_PRODUCTS op ON de.product_id = op.product_id
    162     LEFT JOIN project.ORDERS o ON op.order_id = o.order_id
     269    LEFT JOIN ORDER_PRODUCTS op ON de.product_id = op.product_id
     270    LEFT JOIN ORDERS o ON op.order_id = o.order_id
    163271        AND o.purchase_date > de.date_modified
    164272        AND o.purchase_date <= de.date_modified + INTERVAL '30 days'
     
    179287        post.revenue_after,
    180288        (post.revenue_after - pre.revenue_before) AS net_revenue_impact
     289
    181290    FROM discount_events de
    182     JOIN project.PRODUCTS p ON de.product_id = p.product_id
    183     JOIN project.RELEASES r ON p.release_id = r.release_id
    184     JOIN pre_promo_sales pre ON de.modification_id = pre.modification_id AND de.product_id = pre.product_id
    185     JOIN post_promo_sales post ON de.modification_id = post.modification_id AND de.product_id = post.product_id
     291    JOIN PRODUCTS p ON de.product_id = p.product_id
     292    JOIN RELEASES r ON p.release_id = r.release_id
     293    JOIN pre_promo_sales pre
     294        ON de.modification_id = pre.modification_id
     295       AND de.product_id = pre.product_id
     296    JOIN post_promo_sales post
     297        ON de.modification_id = post.modification_id
     298       AND de.product_id = post.product_id
    186299)
    187300SELECT
     
    197310    revenue_after,
    198311    net_revenue_impact,
     312
    199313    CASE
    200314        WHEN (net_revenue_impact > 0) AND (volume_change > 0) THEN 'SUCCESS: Volume generated profit'
     
    203317        ELSE 'NEUTRAL'
    204318    END AS promotion_verdict
     319
    205320FROM promo_summary
    206321ORDER BY promotion_start_date DESC, net_revenue_impact DESC;
     
    210325
    211326{{{
    212 RELATIONAL ALGEBRA
     327DiscountEvents <-
     328σ m.type_of_modification = 'DISCOUNT'
     329(
     330  MODIFICATIONS m ⨝ (m.modification_id = mp.modification_id) MODIFICATION_PRODUCTS mp
     331)
     332
     333PrePromoSales <-
     334γ modification_id, product_id;
     335  units_sold_before := SUM(op.quantity),
     336  revenue_before := SUM(op.quantity * op.price_at_purchase)
     337(
     338  σ o.purchase_date ∈ [m.date_modified - 30 days, m.date_modified)
     339    ∧ o.status ∈ {PAID, SHIPPED, DELIVERED}
     340  (
     341    DiscountEvents de ⨝ ORDER_PRODUCTS op ⨝ ORDERS o
     342  )
     343)
     344
     345PostPromoSales <-
     346γ modification_id, product_id;
     347  units_sold_after := SUM(op.quantity),
     348  revenue_after := SUM(op.quantity * op.price_at_purchase)
     349(
     350  σ o.purchase_date ∈ (m.date_modified, m.date_modified + 30 days]
     351    ∧ o.status ∈ {PAID, SHIPPED, DELIVERED}
     352  (
     353    DiscountEvents de ⨝ ORDER_PRODUCTS op ⨝ ORDERS o
     354  )
     355)
     356
     357PromoSummary <-
     358π product_id,
     359  release_title := r.title,
     360  product_format := p.format,
     361  promotion_start_date := de.date_modified,
     362  discount_applied := de.discount_percentage,
     363  volume_change := post.units_sold_after - pre.units_sold_before,
     364  net_revenue_impact := post.revenue_after - pre.revenue_before,
     365  units_sold_before := pre.units_sold_before,
     366  units_sold_after := post.units_sold_after,
     367  revenue_before := pre.revenue_before,
     368  revenue_after := post.revenue_after
     369(
     370  DiscountEvents de
     371  ⨝ PRODUCTS p
     372  ⨝ RELEASES r
     373  ⨝ PrePromoSales pre
     374  ⨝ PostPromoSales post
     375)
     376
     377Result <-
     378τ promotion_start_date ↓, net_revenue_impact ↓
     379(
     380  π *,
     381    promotion_verdict :=
     382      CASE
     383        WHEN net_revenue_impact > 0 ∧ volume_change > 0
     384          THEN 'SUCCESS'
     385        WHEN net_revenue_impact < 0 ∧ volume_change > 0
     386          THEN 'MARGIN LOSS'
     387        WHEN volume_change <= 0
     388          THEN 'FAILURE'
     389        ELSE 'NEUTRAL'
     390      END
     391  (
     392    PromoSummary
     393  )
     394)
    213395}}}
    214396
     
    224406
    225407{{{
     408SET search_path TO project;
     409
    226410WITH user_purchase_metrics AS (
    227411    SELECT
     
    231415        COALESCE(SUM(o.points_used), 0) AS lifetime_points_burned,
    232416        COALESCE(SUM(op.quantity * op.price_at_purchase), 0.00) AS total_monetary_spend
    233     FROM project.ORDERS o
    234     LEFT JOIN project.ORDER_PRODUCTS op ON o.order_id = op.order_id
     417
     418    FROM ORDERS o
     419    LEFT JOIN ORDER_PRODUCTS op ON o.order_id = op.order_id
     420
    235421    WHERE o.purchase_date >= CURRENT_DATE - INTERVAL '2 years'
    236422      AND o.status IN ('PAID', 'SHIPPED', 'DELIVERED')
     
    249435        COALESCE(upm.lifetime_points_earned, 0) AS points_earned_2yr,
    250436        COALESCE(upm.lifetime_points_burned, 0) AS points_burned_2yr
    251     FROM project.CONSUMERS c
    252     JOIN project.USERS u ON c.user_id = u.user_id
     437
     438    FROM CONSUMERS c
     439    JOIN USERS u ON c.user_id = u.user_id
    253440    LEFT JOIN user_purchase_metrics upm ON c.user_id = upm.user_id
    254441    WHERE u.date_created >= CURRENT_DATE - INTERVAL '2 years'
     
    273460    points_earned_2yr,
    274461    points_burned_2yr,
     462
    275463    CASE
    276464        WHEN is_vip = 1 THEN 'VIP Tier: High Future Discount Liability'
     
    279467        ELSE 'Standard Active Engagement Profile'
    280468    END AS customer_retention_segment
     469
    281470FROM promo_summary_with_flags
    282471ORDER BY account_creation_vintage DESC, current_unspent_points_balance DESC;
     
    286475
    287476{{{
    288 RELATIONAL ALGEBRA
    289 }}}
    290 
    291 
     477UserPurchaseMetrics <-
     478γ user_id;
     479  total_orders_placed := COUNT(order_id),
     480  lifetime_points_earned := SUM(points_earned),
     481  lifetime_points_burned := SUM(points_used),
     482  total_monetary_spend := SUM(op.quantity * op.price_at_purchase)
     483(
     484  σ o.purchase_date >= NOW - 2 years
     485    ∧ o.status ∈ {PAID, SHIPPED, DELIVERED}
     486  (
     487    ORDERS o ⟕ ORDER_PRODUCTS op
     488  )
     489)
     490
     491PromoSummaryUsers <-
     492π u.user_id,
     493  u.username,
     494  u.email,
     495  account_creation_vintage := EXTRACT(YEAR FROM u.date_created),
     496  registration_date := u.date_created,
     497  orders_count_2yr := COALESCE(um.total_orders_placed, 0),
     498  lifetime_spend_amount := COALESCE(um.total_monetary_spend, 0),
     499  current_unspent_points_balance := c.points_collected,
     500  points_earned_2yr := COALESCE(um.lifetime_points_earned, 0),
     501  points_burned_2yr := COALESCE(um.lifetime_points_burned, 0)
     502(
     503  CONSUMERS c ⨝ USERS u ⟕ UserPurchaseMetrics um
     504)
     505
     506UserFlags <-
     507π *,
     508  is_vip :=
     509    CASE WHEN current_unspent_points_balance ≥ 2000 ∧ orders_count_2yr ≥ 10 THEN 1 ELSE 0 END,
     510  is_hoarder :=
     511    CASE WHEN current_unspent_points_balance ≥ 1000 ∧ points_burned_2yr = 0 THEN 1 ELSE 0 END,
     512  is_churned :=
     513    CASE WHEN points_burned_2yr > 0 ∧ orders_count_2yr ≤ 2 THEN 1 ELSE 0 END
     514(
     515  PromoSummaryUsers
     516)
     517
     518Result <-
     519τ account_creation_vintage ↓, current_unspent_points_balance ↓
     520(
     521  π user_id, username, email,
     522    account_creation_vintage,
     523    registration_date,
     524    orders_count_2yr,
     525    lifetime_spend_amount,
     526    current_unspent_points_balance,
     527    points_earned_2yr,
     528    points_burned_2yr,
     529    customer_retention_segment :=
     530      CASE
     531        WHEN is_vip = 1 THEN 'VIP'
     532        WHEN is_hoarder = 1 THEN 'HOARDER'
     533        WHEN is_churned = 1 THEN 'CHURNED'
     534        ELSE 'STANDARD'
     535      END
     536  (
     537    UserFlags
     538  )
     539)
     540}}}
     541
     542