| 75 | | SQL CODE |
| | 75 | WITH product_sales_6months AS ( |
| | 76 | SELECT |
| | 77 | op.product_id, |
| | 78 | SUM(op.quantity) AS units_sold_6m, |
| | 79 | 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 |
| | 82 | WHERE o.purchase_date >= CURRENT_DATE - INTERVAL '6 months' |
| | 83 | AND o.status IN ('PAID', 'SHIPPED', 'DELIVERED') |
| | 84 | GROUP BY op.product_id |
| | 85 | ), |
| | 86 | product_wishlist_counts AS ( |
| | 87 | SELECT |
| | 88 | product_id, |
| | 89 | COUNT(wishlist_id) AS wishlist_addition_count |
| | 90 | FROM project.WISHLIST_PRODUCTS |
| | 91 | GROUP BY product_id |
| | 92 | ) |
| | 93 | SELECT |
| | 94 | p.product_id, |
| | 95 | r.title AS release_title, |
| | 96 | p.format, |
| | 97 | p.stock AS unsold_stock_quantity, |
| | 98 | p.price AS current_unit_price, |
| | 99 | (p.stock * p.price) AS frozen_capital, |
| | 100 | COALESCE(TO_CHAR(ps.last_purchase_date, 'YYYY-MM-DD'), 'NEVER BOUGHT') AS last_sold_date, |
| | 101 | COALESCE(pw.wishlist_addition_count, 0) AS times_on_wishlists, |
| | 102 | CASE |
| | 103 | WHEN COALESCE(pw.wishlist_addition_count, 0) > 0 THEN 'Discount Target (Wishlisted)' |
| | 104 | ELSE 'Deep Liquidation/Clearance Target' |
| | 105 | END AS inventory_action_plan |
| | 106 | FROM project.PRODUCTS p |
| | 107 | JOIN project.RELEASES r ON p.release_id = r.release_id |
| | 108 | LEFT JOIN product_sales_6months ps ON p.product_id = ps.product_id |
| | 109 | LEFT JOIN product_wishlist_counts pw ON p.product_id = pw.product_id |
| | 110 | WHERE (ps.product_id IS NULL) AND (p.stock > 0) |
| | 111 | ORDER BY frozen_capital DESC, times_on_wishlists DESC; |
| 93 | | SQL CODE |
| | 129 | WITH discount_events AS ( |
| | 130 | SELECT |
| | 131 | m.modification_id, |
| | 132 | m.admin_id, |
| | 133 | m.date_modified, |
| | 134 | m.discount AS discount_percentage, |
| | 135 | mp.product_id |
| | 136 | FROM project.MODIFICATIONS m |
| | 137 | JOIN project.MODIFICATION_PRODUCTS mp ON m.modification_id = mp.modification_id |
| | 138 | WHERE m.type_of_modification = 'DISCOUNT' |
| | 139 | ), |
| | 140 | pre_promo_sales AS ( |
| | 141 | SELECT |
| | 142 | de.modification_id, |
| | 143 | de.product_id, |
| | 144 | COALESCE(SUM(op.quantity), 0) AS units_sold_before, |
| | 145 | COALESCE(SUM(op.quantity * op.price_at_purchase), 0.00) AS revenue_before |
| | 146 | 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 |
| | 149 | AND o.purchase_date >= de.date_modified - INTERVAL '30 days' |
| | 150 | AND o.purchase_date < de.date_modified |
| | 151 | AND o.status IN ('PAID', 'SHIPPED', 'DELIVERED') |
| | 152 | GROUP BY de.modification_id, de.product_id |
| | 153 | ), |
| | 154 | post_promo_sales AS ( |
| | 155 | SELECT |
| | 156 | de.modification_id, |
| | 157 | de.product_id, |
| | 158 | COALESCE(SUM(op.quantity), 0) AS units_sold_after, |
| | 159 | COALESCE(SUM(op.quantity * op.price_at_purchase), 0.00) AS revenue_after |
| | 160 | 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 |
| | 163 | AND o.purchase_date > de.date_modified |
| | 164 | AND o.purchase_date <= de.date_modified + INTERVAL '30 days' |
| | 165 | AND o.status IN ('PAID', 'SHIPPED', 'DELIVERED') |
| | 166 | GROUP BY de.modification_id, de.product_id |
| | 167 | ), |
| | 168 | promo_summary AS ( |
| | 169 | SELECT |
| | 170 | de.product_id, |
| | 171 | r.title AS release_title, |
| | 172 | p.format AS product_format, |
| | 173 | de.date_modified AS promotion_start_date, |
| | 174 | de.discount_percentage AS discount_applied, |
| | 175 | pre.units_sold_before, |
| | 176 | post.units_sold_after, |
| | 177 | (post.units_sold_after - pre.units_sold_before) AS volume_change, |
| | 178 | pre.revenue_before, |
| | 179 | post.revenue_after, |
| | 180 | (post.revenue_after - pre.revenue_before) AS net_revenue_impact |
| | 181 | 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 |
| | 186 | ) |
| | 187 | SELECT |
| | 188 | product_id, |
| | 189 | release_title, |
| | 190 | product_format, |
| | 191 | promotion_start_date, |
| | 192 | discount_applied, |
| | 193 | units_sold_before, |
| | 194 | units_sold_after, |
| | 195 | volume_change, |
| | 196 | revenue_before, |
| | 197 | revenue_after, |
| | 198 | net_revenue_impact, |
| | 199 | CASE |
| | 200 | WHEN (net_revenue_impact > 0) AND (volume_change > 0) THEN 'SUCCESS: Volume generated profit' |
| | 201 | WHEN (net_revenue_impact < 0) AND (volume_change > 0) THEN 'MARGIN LOSS: Volume rose but lost overall revenue' |
| | 202 | WHEN (volume_change <= 0) THEN 'FAILURE: No demand increase observed' |
| | 203 | ELSE 'NEUTRAL' |
| | 204 | END AS promotion_verdict |
| | 205 | FROM promo_summary |
| | 206 | ORDER BY promotion_start_date DESC, net_revenue_impact DESC; |
| 113 | | SQL CODE |
| 114 | | }}} |
| 115 | | |
| 116 | | ==== Relational Algebra |
| 117 | | |
| 118 | | {{{ |
| 119 | | RELATIONAL ALGEBRA |
| 120 | | }}} |
| 121 | | |
| 122 | | |
| | 226 | WITH user_purchase_metrics AS ( |
| | 227 | SELECT |
| | 228 | o.user_id, |
| | 229 | COUNT(o.order_id) AS total_orders_placed, |
| | 230 | COALESCE(SUM(o.points_earned), 0) AS lifetime_points_earned, |
| | 231 | COALESCE(SUM(o.points_used), 0) AS lifetime_points_burned, |
| | 232 | 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 |
| | 235 | WHERE o.purchase_date >= CURRENT_DATE - INTERVAL '2 years' |
| | 236 | AND o.status IN ('PAID', 'SHIPPED', 'DELIVERED') |
| | 237 | GROUP BY o.user_id |
| | 238 | ), |
| | 239 | promo_summary AS ( |
| | 240 | SELECT |
| | 241 | u.user_id, |
| | 242 | u.username, |
| | 243 | u.email, |
| | 244 | EXTRACT(YEAR FROM u.date_created) AS account_creation_vintage, |
| | 245 | u.date_created AS registration_date, |
| | 246 | COALESCE(upm.total_orders_placed, 0) AS orders_count_2yr, |
| | 247 | ROUND(COALESCE(upm.total_monetary_spend, 0.00), 2) AS lifetime_spend_amount, |
| | 248 | c.points_collected AS current_unspent_points_balance, |
| | 249 | COALESCE(upm.lifetime_points_earned, 0) AS points_earned_2yr, |
| | 250 | 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 |
| | 253 | LEFT JOIN user_purchase_metrics upm ON c.user_id = upm.user_id |
| | 254 | WHERE u.date_created >= CURRENT_DATE - INTERVAL '2 years' |
| | 255 | ), |
| | 256 | promo_summary_with_flags AS ( |
| | 257 | SELECT |
| | 258 | *, |
| | 259 | CASE WHEN current_unspent_points_balance >= 2000 AND orders_count_2yr >= 10 THEN 1 ELSE 0 END AS is_vip, |
| | 260 | CASE WHEN current_unspent_points_balance >= 1000 AND points_burned_2yr = 0 THEN 1 ELSE 0 END AS is_hoarder, |
| | 261 | CASE WHEN points_burned_2yr > 0 AND orders_count_2yr <= 2 THEN 1 ELSE 0 END AS is_churned |
| | 262 | FROM promo_summary |
| | 263 | ) |
| | 264 | SELECT |
| | 265 | user_id, |
| | 266 | username, |
| | 267 | email, |
| | 268 | account_creation_vintage, |
| | 269 | registration_date, |
| | 270 | orders_count_2yr, |
| | 271 | lifetime_spend_amount, |
| | 272 | current_unspent_points_balance, |
| | 273 | points_earned_2yr, |
| | 274 | points_burned_2yr, |
| | 275 | CASE |
| | 276 | WHEN is_vip = 1 THEN 'VIP Tier: High Future Discount Liability' |
| | 277 | WHEN is_hoarder = 1 THEN 'Points Hoarder: Inactive Burn (High Risk)' |
| | 278 | WHEN is_churned = 1 THEN 'Churned After Reward Use (No Retention)' |
| | 279 | ELSE 'Standard Active Engagement Profile' |
| | 280 | END AS customer_retention_segment |
| | 281 | FROM promo_summary_with_flags |
| | 282 | ORDER BY account_creation_vintage DESC, current_unspent_points_balance DESC; |
| | 283 | }}} |
| | 284 | |
| | 285 | ==== Relational Algebra |
| | 286 | |
| | 287 | {{{ |
| | 288 | RELATIONAL ALGEBRA |
| | 289 | }}} |
| | 290 | |
| | 291 | |