Changes between Version 11 and Version 12 of P6
- Timestamp:
- 04/28/26 10:43:42 (4 days ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
P6
v11 v12 51 51 / NULLIF(total_chapters, 0), 2 52 52 ) AS avg_views_per_chapter, 53 LAG(total_views) OVER (PARTITION BY story_id ORDER BY quarter) 54 AS prev_quarter_views, 55 LAG(total_likes) OVER (PARTITION BY story_id ORDER BY quarter) 56 AS prev_quarter_likes, 57 LAG(total_comments) OVER (PARTITION BY story_id ORDER BY quarter) 58 AS prev_quarter_comments 53 LAG(total_views) OVER (PARTITION BY story_id ORDER BY quarter) AS prev_quarter_views, 54 LAG(total_likes) OVER (PARTITION BY story_id ORDER BY quarter) AS prev_quarter_likes, 55 LAG(total_comments) OVER (PARTITION BY story_id ORDER BY quarter) AS prev_quarter_comments 59 56 FROM quarterly_story_stats 60 57 ), … … 65 62 (total_views - prev_quarter_views)::DECIMAL 66 63 / NULLIF(prev_quarter_views, 0) * 100, 2 67 ) AS views_growth_pct,64 ) AS views_growth_pct, 68 65 ROUND( 69 66 (total_likes - prev_quarter_likes)::DECIMAL 70 67 / NULLIF(prev_quarter_likes, 0) * 100, 2 71 ) AS likes_growth_pct,68 ) AS likes_growth_pct, 72 69 ROUND( 73 70 (total_comments - prev_quarter_comments)::DECIMAL 74 71 / NULLIF(prev_quarter_comments, 0) * 100, 2 75 ) AS comments_growth_pct72 ) AS comments_growth_pct 76 73 FROM with_engagement 77 74 ) 78 75 SELECT 79 TO_CHAR(quarter, 'YYYY "Q"Q') AS period,76 TO_CHAR(quarter, 'YYYY "Q"Q') AS period, 80 77 writer, 81 78 story_id, … … 147 144 QuarterlyStats ← 148 145 γ 149 quarter := DATE_TRUNC('quarter', s.story_created_at),150 story_id := s.story_id,146 quarter := DATE_TRUNC('quarter', s.story_created_at), 147 story_id := s.story_id, 151 148 short_description := s.short_description, 152 mature_content := s.mature_content,153 user_id := u.user_id,154 writer := u.username,155 status := st.status;156 total_chapters := COUNT(DISTINCT ch.chapter_id),157 total_views := COALESCE(SUM(ch.view_count), 0),158 total_words := COALESCE(SUM(ch.word_count), 0),159 avg_rating := ROUND(AVG(ch.rating), 2),160 total_likes := COUNT(DISTINCT l.user_id),161 total_comments := COUNT(DISTINCT c.comment_id),149 mature_content := s.mature_content, 150 user_id := u.user_id, 151 writer := u.username, 152 status := st.status; 153 total_chapters := COUNT(DISTINCT ch.chapter_id), 154 total_views := COALESCE(SUM(ch.view_count), 0), 155 total_words := COALESCE(SUM(ch.word_count), 0), 156 avg_rating := ROUND(AVG(ch.rating), 2), 157 total_likes := COUNT(DISTINCT l.user_id), 158 total_comments := COUNT(DISTINCT c.comment_id), 162 159 total_collaborators := COUNT(DISTINCT col.user_id), 163 total_genres := COUNT(DISTINCT hg.genre_id),160 total_genres := COUNT(DISTINCT hg.genre_id), 164 161 saved_in_lists := COUNT(DISTINCT rli.list_id) 165 162 ( … … 170 167 π 171 168 *, 172 ROUND((total_likes + total_comments) / NULLIF(total_views, 0) * 100, 2) 173 → engagement_rate, 174 ROUND(total_views / NULLIF(total_chapters, 0), 2) 175 → avg_views_per_chapter, 176 LAG(total_views) OVER (PARTITION BY story_id ORDER BY quarter) 177 → prev_quarter_views, 178 LAG(total_likes) OVER (PARTITION BY story_id ORDER BY quarter) 179 → prev_quarter_likes, 180 LAG(total_comments) OVER (PARTITION BY story_id ORDER BY quarter) 181 → prev_quarter_comments 169 ROUND((total_likes + total_comments) / NULLIF(total_views, 0) * 100, 2) → engagement_rate, 170 ROUND(total_views / NULLIF(total_chapters, 0), 2) → avg_views_per_chapter, 171 LAG(total_views) OVER (PARTITION BY story_id ORDER BY quarter) → prev_quarter_views, 172 LAG(total_likes) OVER (PARTITION BY story_id ORDER BY quarter) → prev_quarter_likes, 173 LAG(total_comments) OVER (PARTITION BY story_id ORDER BY quarter) → prev_quarter_comments 182 174 ( 183 175 QuarterlyStats … … 190 182 (total_views - prev_quarter_views) 191 183 / NULLIF(prev_quarter_views, 0) * 100, 2 192 ) → views_growth_pct,184 ) → views_growth_pct, 193 185 ROUND( 194 186 (total_likes - prev_quarter_likes) 195 187 / NULLIF(prev_quarter_likes, 0) * 100, 2 196 ) → likes_growth_pct,188 ) → likes_growth_pct, 197 189 ROUND( 198 190 (total_comments - prev_quarter_comments) 199 191 / NULLIF(prev_quarter_comments, 0) * 100, 2 200 ) → comments_growth_pct192 ) → comments_growth_pct 201 193 ( 202 194 WithEngagement … … 205 197 Result ← 206 198 π 207 TO_CHAR(quarter, 'YYYY "Q"Q') → period,199 TO_CHAR(quarter, 'YYYY "Q"Q') → period, 208 200 writer, 209 201 story_id, … … 218 210 total_views, 219 211 avg_views_per_chapter, 220 COALESCE(views_growth_pct, 0) → views_growth_pct,212 COALESCE(views_growth_pct, 0) → views_growth_pct, 221 213 total_likes, 222 COALESCE(likes_growth_pct, 0) → likes_growth_pct,214 COALESCE(likes_growth_pct, 0) → likes_growth_pct, 223 215 total_comments, 224 COALESCE(comments_growth_pct, 0) → comments_growth_pct, 225 COALESCE(avg_rating, 0) → avg_rating, 226 COALESCE(engagement_rate, 0) → engagement_rate, 227 RANK() OVER (PARTITION BY quarter ORDER BY total_views DESC) 228 → rank_by_views, 229 RANK() OVER (PARTITION BY quarter ORDER BY engagement_rate DESC) 230 → rank_by_engagement, 231 RANK() OVER (PARTITION BY quarter ORDER BY avg_rating DESC) 232 → rank_by_rating 216 COALESCE(comments_growth_pct, 0) → comments_growth_pct, 217 COALESCE(avg_rating, 0 → avg_rating, 218 COALESCE(engagement_rate, 0) → engagement_rate, 219 RANK() OVER (PARTITION BY quarter ORDER BY total_views DESC) → rank_by_views, 220 RANK() OVER (PARTITION BY quarter ORDER BY engagement_rate DESC) → rank_by_engagement, 221 RANK() OVER (PARTITION BY quarter ORDER BY avg_rating DESC) → rank_by_rating 233 222 ( 234 223 WithGrowth … … 251 240 COUNT(DISTINCT c.comment_id) AS total_comments, 252 241 ROUND(AVG(ch.rating), 2) AS avg_rating 253 FROM genre g 254 JOIN has_genre hg ON g.genre_id = hg.genre_id 255 JOIN story s ON hg.story_id = s.story_id 256 JOIN writer w ON s.user_id = w.user_id 257 JOIN status st ON s.story_id = st.story_id 258 AND st.status = 'published' 259 LEFT JOIN chapter ch ON s.story_id = ch.story_id 260 LEFT JOIN likes l ON s.story_id = l.story_id 261 LEFT JOIN comment c ON s.story_id = c.story_id 242 FROM genre g 243 JOIN has_genre hg ON g.genre_id = hg.genre_id 244 JOIN story s ON hg.story_id = s.story_id 245 JOIN writer w ON s.user_id = w.user_id 246 JOIN status st ON s.story_id = st.story_id AND st.status = 'published' 247 LEFT JOIN chapter ch ON s.story_id = ch.story_id 248 LEFT JOIN likes l ON s.story_id = l.story_id 249 LEFT JOIN comment c ON s.story_id = c.story_id 262 250 GROUP BY 263 251 DATE_TRUNC('year', s.story_created_at), … … 343 331 genre_id := g.genre_id, 344 332 genre_name := g.genre_name; 345 total_stories := COUNT(DISTINCT s.story_id),346 total_writers := COUNT(DISTINCT w.user_id),347 total_views := COALESCE(SUM(ch.view_count), 0),348 total_words := COALESCE(SUM(ch.word_count), 0),349 total_likes := COUNT(DISTINCT l.user_id),333 total_stories := COUNT(DISTINCT s.story_id), 334 total_writers := COUNT(DISTINCT w.user_id), 335 total_views := COALESCE(SUM(ch.view_count), 0), 336 total_words := COALESCE(SUM(ch.word_count), 0), 337 total_likes := COUNT(DISTINCT l.user_id), 350 338 total_comments := COUNT(DISTINCT c.comment_id), 351 avg_rating := ROUND(AVG(ch.rating), 2)339 avg_rating := ROUND(AVG(ch.rating), 2) 352 340 ( 353 341 WithComments … … 366 354 total_comments, 367 355 avg_rating, 368 ROUND((total_likes + total_comments) / NULLIF(total_views, 0) * 100, 2) 369 → engagement_rate, 370 ROUND(total_views / NULLIF(total_stories, 0), 2) 371 → avg_views_per_story, 372 LAG(total_views) OVER (PARTITION BY genre_id ORDER BY year) 373 → prev_year_views, 374 LAG(total_stories) OVER (PARTITION BY genre_id ORDER BY year) 375 → prev_year_stories 356 ROUND((total_likes + total_comments) / NULLIF(total_views, 0) * 100, 2) → engagement_rate, 357 ROUND(total_views / NULLIF(total_stories, 0), 2) → avg_views_per_story, 358 LAG(total_views) OVER (PARTITION BY genre_id ORDER BY year) → prev_year_views, 359 LAG(total_stories) OVER (PARTITION BY genre_id ORDER BY year) → prev_year_stories 376 360 ( 377 361 GenreAnnual … … 380 364 Result ← 381 365 π 382 TO_CHAR(year, 'YYYY') → year,366 TO_CHAR(year, 'YYYY') → year, 383 367 genre_name, 384 368 total_stories, … … 388 372 total_likes, 389 373 total_comments, 390 COALESCE(avg_rating, 0) → avg_rating,391 COALESCE(engagement_rate, 0) → engagement_rate,374 COALESCE(avg_rating, 0) → avg_rating, 375 COALESCE(engagement_rate, 0) → engagement_rate, 392 376 ROUND( 393 377 (total_views - prev_year_views) 394 378 / NULLIF(prev_year_views, 0) * 100, 2 395 ) → yoy_views_growth_pct,379 ) → yoy_views_growth_pct, 396 380 ROUND( 397 381 (total_stories - prev_year_stories) 398 382 / NULLIF(prev_year_stories, 0) * 100, 2 399 ) → yoy_stories_growth_pct, 400 RANK() OVER (PARTITION BY year ORDER BY total_views DESC) 401 → popularity_rank, 402 RANK() OVER (PARTITION BY year ORDER BY engagement_rate DESC) 403 → engagement_rank 383 ) → yoy_stories_growth_pct, 384 RANK() OVER (PARTITION BY year ORDER BY total_views DESC) → popularity_rank, 385 RANK() OVER (PARTITION BY year ORDER BY engagement_rate DESC) → engagement_rank 404 386 ( 405 387 WithMetrics … … 412 394 WITH quarterly_stats AS ( 413 395 SELECT 414 DATE_TRUNC('quarter', s.story_created_at) AS quarter,396 DATE_TRUNC('quarter', s.story_created_at) AS quarter, 415 397 u.user_id, 416 398 u.username, … … 424 406 COUNT(DISTINCT c.comment_id) AS total_comments, 425 407 ROUND(AVG(ch.rating), 2) AS avg_rating 426 FROM story s 427 JOIN writer w ON s.user_id = w.user_id 428 JOIN users u ON w.user_id = u.user_id 429 JOIN status st ON s.story_id = st.story_id 430 AND st.status = 'published' 431 LEFT JOIN chapter ch ON s.story_id = ch.story_id 432 LEFT JOIN likes l ON s.story_id = l.story_id 433 LEFT JOIN comment c ON s.story_id = c.story_id 408 FROM story s 409 JOIN writer w ON s.user_id = w.user_id 410 JOIN users u ON w.user_id = u.user_id 411 JOIN status st ON s.story_id = st.story_id AND st.status = 'published' 412 LEFT JOIN chapter ch ON s.story_id = ch.story_id 413 LEFT JOIN likes l ON s.story_id = l.story_id 414 LEFT JOIN comment c ON s.story_id = c.story_id 434 415 GROUP BY 435 416 DATE_TRUNC('quarter', s.story_created_at), … … 439 420 SELECT 440 421 *, 441 LAG(total_views) OVER (PARTITION BY user_id ORDER BY quarter) AS prev_views,442 LAG(total_likes) OVER (PARTITION BY user_id ORDER BY quarter) AS prev_likes,422 LAG(total_views) OVER (PARTITION BY user_id ORDER BY quarter) AS prev_views, 423 LAG(total_likes) OVER (PARTITION BY user_id ORDER BY quarter) AS prev_likes, 443 424 LAG(total_comments) OVER (PARTITION BY user_id ORDER BY quarter) AS prev_comments, 444 425 ROUND( … … 502 483 QuarterlyStats ← 503 484 γ 504 quarter := DATE_TRUNC('quarter', s.story_created_at),505 user_id := u.user_id,506 username := u.username,507 user_name := u.user_name,508 surname := u.surname;485 quarter := DATE_TRUNC('quarter', s.story_created_at), 486 user_id := u.user_id, 487 username := u.username, 488 user_name := u.user_name, 489 surname := u.surname; 509 490 stories_published := COUNT(DISTINCT s.story_id), 510 491 chapters_written := COUNT(DISTINCT ch.chapter_id), 511 total_views := COALESCE(SUM(ch.view_count), 0),512 total_words := COALESCE(SUM(ch.word_count), 0),513 total_likes := COUNT(DISTINCT l.user_id),514 total_comments := COUNT(DISTINCT c.comment_id),515 avg_rating := ROUND(AVG(ch.rating), 2)492 total_views := COALESCE(SUM(ch.view_count), 0), 493 total_words := COALESCE(SUM(ch.word_count), 0), 494 total_likes := COUNT(DISTINCT l.user_id), 495 total_comments := COUNT(DISTINCT c.comment_id), 496 avg_rating := ROUND(AVG(ch.rating), 2) 516 497 ( 517 498 WithComments … … 532 513 total_comments, 533 514 avg_rating, 534 LAG(total_views) OVER (PARTITION BY user_id ORDER BY quarter) 535 → prev_views, 536 LAG(total_likes) OVER (PARTITION BY user_id ORDER BY quarter) 537 → prev_likes, 538 LAG(total_comments) OVER (PARTITION BY user_id ORDER BY quarter) 539 → prev_comments, 515 LAG(total_views) OVER (PARTITION BY user_id ORDER BY quarter) → prev_views, 516 LAG(total_likes) OVER (PARTITION BY user_id ORDER BY quarter) → prev_likes, 517 LAG(total_comments) OVER (PARTITION BY user_id ORDER BY quarter) → prev_comments, 540 518 ROUND( 541 519 (total_views - LAG(total_views) OVER (PARTITION BY user_id ORDER BY quarter)) 542 520 / NULLIF(LAG(total_views) OVER (PARTITION BY user_id ORDER BY quarter), 0) 543 521 * 100, 2 544 ) → views_growth_pct,522 ) → views_growth_pct, 545 523 ROUND( 546 524 (total_likes - LAG(total_likes) OVER (PARTITION BY user_id ORDER BY quarter)) 547 525 / NULLIF(LAG(total_likes) OVER (PARTITION BY user_id ORDER BY quarter), 0) 548 526 * 100, 2 549 ) → likes_growth_pct527 ) → likes_growth_pct 550 528 ( 551 529 QuarterlyStats … … 554 532 Result ← 555 533 π 556 TO_CHAR(quarter, 'YYYY "Q"Q') → period,534 TO_CHAR(quarter, 'YYYY "Q"Q') → period, 557 535 username, 558 536 user_name, … … 562 540 total_words, 563 541 total_views, 564 COALESCE(views_growth_pct, 0) → views_growth_pct,542 COALESCE(views_growth_pct, 0) → views_growth_pct, 565 543 total_likes, 566 COALESCE(likes_growth_pct, 0) → likes_growth_pct,544 COALESCE(likes_growth_pct, 0) → likes_growth_pct, 567 545 total_comments, 568 COALESCE(avg_rating, 0) → avg_rating, 569 RANK() OVER (PARTITION BY quarter ORDER BY total_views DESC) 570 → rank_by_views 546 COALESCE(avg_rating, 0) → avg_rating, 547 RANK() OVER (PARTITION BY quarter ORDER BY total_views DESC) → rank_by_views 571 548 ( 572 549 WithGrowth
