| 33 | |
| 34 | == Подредување на категориите според средна оценка од сите бизниси кои спаѓаат во нив (во која категорија се најдобро оценетите бизниси), по тромесечие |
| 35 | |
| 36 | {{{#!sql |
| 37 | drop view if exists best_rated_businesses_by_trimesters cascade; |
| 38 | create view best_rated_businesses_by_trimesters as |
| 39 | |
| 40 | |
| 41 | SELECT DISTINCT cc.category_id, |
| 42 | cc.category_name, |
| 43 | |
| 44 | |
| 45 | |
| 46 | CASE WHEN tabela.quarter=1 THEN tabela.category_avg_stars ELSE 0 END AS first_q, |
| 47 | CASE WHEN tabela.quarter=2 THEN tabela.category_avg_stars ELSE 0 END AS second_q, |
| 48 | CASE WHEN tabela.quarter=3 THEN tabela.category_avg_stars ELSE 0 END AS third_q, |
| 49 | CASE WHEN tabela.quarter=4 THEN tabela.category_avg_stars ELSE 0 END AS fourth_q |
| 50 | |
| 51 | |
| 52 | from business bb |
| 53 | join category cc on |
| 54 | cc.category_id = bb.category_id |
| 55 | join review rr on |
| 56 | rr.business_id = bb.business_id |
| 57 | |
| 58 | join ( |
| 59 | select distinct c2.category_id, c2.category_name, |
| 60 | extract(quarter FROM r2.review_timestamp) AS quarter, |
| 61 | avg(r2.review_stars) as category_avg_stars |
| 62 | from business b2 |
| 63 | join category c2 on |
| 64 | b2.category_id = c2.category_id |
| 65 | join review r2 on |
| 66 | r2.business_id = b2.business_id |
| 67 | group by 1,2,3 |
| 68 | ) as tabela on cc.category_id = tabela.category_id |
| 69 | |
| 70 | }}} |