-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAd Hoc requests.sql
More file actions
189 lines (164 loc) · 5.71 KB
/
Ad Hoc requests.sql
File metadata and controls
189 lines (164 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use rp;
select * from dim_city;
select * from dim_ad_category;
select * from fact_ad_revenue;
select * from fact_city_readiness;
select * from fact_print_sales;
select * from fact_digital_pilot;
-- Business Request – 1: Monthly Circulation Drop Check
-- Generate a report showing the top 3 months (2019–2024) where any city recorded the
-- sharpest month-over-month decline in net_circulation.
with circulation as (
select city_id, month, net_circulation,
lag(net_circulation)over(partition by city_id order by month) as prev_net_circulation,
(net_circulation - lag(net_circulation)over(partition by city_id order by month)) as mom
from fact_print_sales
)
select city_id, month, prev_net_circulation,
net_circulation AS current_net_circulation,
mom AS decline_value
FROM circulation
WHERE mom < 0
ORDER BY mom ASC
LIMIT 3;
drop table fact_ad_revenue;
use rp;
select * from fact_ad_revenue;
with cte as (
select year,
ad_category as category_name,
sum(ad_revenue) as total_revenue,
sum(sum(ad_revenue)) over (partition by year) as total_revenue_by_year
from fact_ad_revenue
group by year, ad_category
),
cte1 as (
select year,
category_name,
total_revenue,
total_revenue_by_year,
(total_revenue * 100.0 / total_revenue_by_year) as pct_of_year
from cte
)
select year, category_name, total_revenue
from cte1
where pct_of_year > 50
order by year;
-- Business Request – 3: 2024 Print Efficiency Leaderboard
-- For 2024, rank cities by print efficiency = net_circulation / copies_printed. Return top 5.
select * from dim_city;
select * from fact_print_sales ;
SELECT
d.city AS city_name,
SUM(f.copies_sold - f.copies_returned) AS copies_printed_2024,
SUM(f.net_circulation) AS net_circulation_2024,
ROUND(SUM(f.net_circulation) * 1.0 / SUM(f.copies_sold - f.copies_returned), 2) AS efficiency_ratio,
RANK() OVER (ORDER BY SUM(f.net_circulation) * 1.0 / SUM(f.copies_sold - f.copies_returned) DESC) AS efficiency_rank_2024
FROM fact_print_sales f
JOIN dim_city d
ON f.city_id = d.city_id
WHERE f.month LIKE '%24'
GROUP BY d.city
ORDER BY efficiency_rank_2024
LIMIT 5;
-- Business Request – 4 : Internet Readiness Growth (2021)
-- For each city, compute the change in internet penetration from Q1-2021 to Q4-2021
-- and identify the city with the highest improvement.
select
c.city as city_name,
q1.internet_penetration as internet_rate_q1_2021,
q4.internet_penetration as internet_rate_q4_2021,
(q4.internet_penetration - q1.internet_penetration) as delta_internet_rate
from dim_city c
join fact_city_readiness q1
on c.city_id = q1.city_id and q1.quarter = '2021-Q1'
join fact_city_readiness q4
on c.city_id = q4.city_id and q4.quarter = '2021-Q4'
order by delta_internet_rate desc
limit 1;
use rp;
-- Print Circulation Trends
-- What is the trend in copies printed, copies sold, and net circulation across all
-- cities from 2019 to 2024? How has this changed year-over-year?
select month from fact_print_sales;
WITH yearly_summary AS (
SELECT
RIGHT(month, 4) AS year,
SUM(copies_sold + copies_returned) AS total_copies_printed,
SUM(copies_sold) AS total_copies_sold,
SUM(net_circulation) AS total_net_circulation
FROM fact_print_sales
GROUP BY RIGHT(month, 4)
)
SELECT
year,
total_copies_printed,
total_copies_sold,
total_net_circulation,
ROUND(
(total_net_circulation - LAG(total_net_circulation) OVER (ORDER BY year))
/ NULLIF(LAG(total_net_circulation) OVER (ORDER BY year), 0) * 100,
2
) AS yoy_change_percent
FROM yearly_summary
ORDER BY year;
-- Which cities contributed the highest to net circulation and copies sold in 2024?
-- Are these cities still profitable to operate in?
SELECT
d.city,
SUM(fps.net_circulation) AS total_net_circulation,
SUM(fps.copies_sold) AS total_copies_sold
FROM fact_print_sales fps
JOIN dim_city d
ON fps.city_id = d.city_id
WHERE month like '%2024%'
GROUP BY d.city
ORDER BY total_net_circulation DESC
LIMIT 10;
-- Print Waste Analysis
-- Which cities have the largest gap between copies printed and net circulation, and
-- how has that gap changed over time?
SELECT
d.city,
RIGHT(month, 4) AS year,
SUM((fps.copies_returned+fps.copies_sold) - fps.net_circulation) AS total_print_waste,
ROUND(SUM((fps.copies_returned+fps.copies_sold) - fps.net_circulation)
/ NULLIF(SUM(fps.copies_returned+fps.copies_sold), 0) * 100, 2) AS waste_percent
FROM fact_print_sales fps
JOIN dim_city d
ON fps.city_id = d.city_id
GROUP BY d.city, year
ORDER BY total_print_waste DESC;
-- Ad Revenue Trends by Category (2019–2024)
SELECT
year,
dac.standard_ad_category,
SUM(far.ad_revenue) AS total_revenue,
ROUND(
(SUM(far.ad_revenue)
- LAG(SUM(far.ad_revenue)) OVER (PARTITION BY dac.standard_ad_category ORDER BY year))
/ NULLIF(LAG(SUM(far.ad_revenue)) OVER (PARTITION BY dac.standard_ad_category ORDER BY year), 0) * 100, 2
) AS yoy_growth_percent
FROM fact_ad_revenue far
LEFT JOIN dim_ad_category dac
ON far.ad_category = dac.ad_category_id
GROUP BY year, dac.standard_ad_category
ORDER BY dac.standard_ad_category, year;
-- 7. Ad Revenue vs Circulation ROI (Yearly)
SELECT
d.city,
year,
SUM(far.ad_revenue) AS total_revenue,
SUM(fps.net_circulation) AS total_circulation,
ROUND(
SUM(far.ad_revenue) / NULLIF(SUM(fps.net_circulation), 0),
2
) AS revenue_per_copy
FROM fact_ad_revenue far
JOIN dim_city d
ON far.edition_id = d.city_id
LEFT JOIN fact_print_sales fps
ON far.edition_id = fps.city_id
AND year = RIGHT(month, 4)
GROUP BY d.city, year
ORDER BY revenue_per_copy DESC;