-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDonation.sql
More file actions
195 lines (165 loc) · 4.68 KB
/
Donation.sql
File metadata and controls
195 lines (165 loc) · 4.68 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
189
-- DROP old views first
DROP VIEW IF EXISTS
total_donations_per_fund,
donations_over_time,
top_10_donors,
donations_by_state,
yearly_totals_with_pct_change,
donor_retention,
avg_donation_per_fund,
avg_donation_per_donor_by_state,
fund_popularity;
-- Create the cleaned table in one shot:
-- Renames columns
-- Filters bad rows
-- Converts donation_date to DATE immediately
DROP TABLE IF EXISTS cleaned_donations;
CREATE TABLE cleaned_donations AS
SELECT
name AS donor_name,
recipname AS fund_name,
TO_DATE(date, 'MM/DD/YYYY') AS donation_date,
amnt AS donation_amount,
city AS donor_city,
state AS donor_state,
zip AS donor_zip
FROM raw_campaign_contributions
WHERE amnt > 0 AND name IS NOT NULL;
-- Report A: Total donations grouped by each fund
CREATE VIEW total_donations_per_fund AS
SELECT
fund_name, -- The fund receiving the donations
COUNT(*) AS num_donations, -- How many individual donations it received
SUM(donation_amount) AS total_donated -- Total dollar amount donated to this fund
FROM
cleaned_donations -- Use your cleaned table
GROUP BY
fund_name -- Group results by fund name
ORDER BY
total_donated DESC; -- Show the largest total donations first
-- Report B: Total donations per month (trend)
CREATE VIEW donations_over_time AS
SELECT
DATE_TRUNC('month', donation_date) AS month,
SUM(donation_amount) AS total_donated
FROM
cleaned_donations
GROUP BY
month
ORDER BY
month;
-- Report C: Find the top 10 individual donors by total contribution amount
CREATE VIEW top_10_donors AS
SELECT
donor_name, -- Donor's name
SUM(donation_amount) AS total_donated -- Total amount they donated
FROM
cleaned_donations
GROUP BY
donor_name -- Group by donor name
ORDER BY
total_donated DESC -- Sort by biggest donor first
LIMIT 10; -- Only return top 10 rows
-- Report D: Summary of donations by donor's state (good for maps!)
CREATE VIEW donations_by_state AS
SELECT
donor_state, -- US State
COUNT(*) AS num_donations, -- How many donations came from this state
SUM(donation_amount) AS total_donated -- Total dollar amount from this state
FROM
cleaned_donations
GROUP BY
donor_state -- Group by state
ORDER BY
total_donated DESC; -- Show states with highest donations first
-- Report E: Year-over-Year donation totals with % change
CREATE VIEW yearly_totals_with_pct_change AS
WITH yearly_totals AS (
SELECT
EXTRACT(YEAR FROM donation_date)::INT AS donation_year,
SUM(donation_amount) AS total_donated
FROM
cleaned_donations
GROUP BY
donation_year
)
SELECT
donation_year,
total_donated,
LAG(total_donated) OVER (ORDER BY donation_year) AS prev_year_total,
ROUND(
100.0 * (total_donated - LAG(total_donated) OVER (ORDER BY donation_year))
/ NULLIF(LAG(total_donated) OVER (ORDER BY donation_year), 0), 2
) AS pct_change
FROM
yearly_totals
ORDER BY
donation_year;
-- Report F: Donors retention: donors who gave in consecutive calendar years
CREATE VIEW donor_retention AS
WITH donor_years AS (
SELECT DISTINCT
donor_name,
EXTRACT(YEAR FROM donation_date)::INT AS donation_year
FROM
cleaned_donations
),
repeat_donors AS (
SELECT
d1.donor_name,
d1.donation_year AS year,
d2.donation_year AS next_year
FROM
donor_years d1
JOIN donor_years d2
ON d1.donor_name = d2.donor_name
AND d2.donation_year = d1.donation_year + 1
)
SELECT
year,
COUNT(DISTINCT donor_name) AS repeat_donor_count
FROM
repeat_donors
GROUP BY
year
ORDER BY
year;
-- Report G: Average donation amount per fund
CREATE VIEW avg_donation_per_fund AS
SELECT
fund_name,
COUNT(*) AS num_donations,
SUM(donation_amount) AS total_donated,
ROUND(AVG(donation_amount), 2) AS avg_donation
FROM
cleaned_donations
GROUP BY
fund_name
ORDER BY
avg_donation DESC;
-- Report H: Average donation per donor by state
CREATE VIEW avg_donation_per_donor_by_state AS
SELECT
donor_state,
COUNT(DISTINCT donor_name) AS num_unique_donors,
SUM(donation_amount) AS total_donated,
ROUND(SUM(donation_amount) / NULLIF(COUNT(DISTINCT donor_name),0), 2) AS avg_per_donor
FROM
cleaned_donations
GROUP BY
donor_state
ORDER BY
avg_per_donor DESC;
-- Report I: Total and unique donors per fund
CREATE VIEW fund_popularity AS
SELECT
fund_name,
COUNT(*) AS num_donations,
COUNT(DISTINCT donor_name) AS unique_donors,
SUM(donation_amount) AS total_donated
FROM
cleaned_donations
GROUP BY
fund_name
ORDER BY
total_donated DESC;