| 1 | = Advanced database reports (SQL and stored procedures) = |
| 2 | |
| 3 | == Client Reports by Quarter == |
| 4 | This report provides a summary of client marketing activities by quarter, including the total number of requests, the total target goals, and the progress made for each type of marketing request (Profile Marketing Request and Post Marketing Request). |
| 5 | |
| 6 | {{{ |
| 7 | SELECT |
| 8 | u.UserID, |
| 9 | u.Username, |
| 10 | EXTRACT(QUARTER FROM pmr.Date_Created) AS Quarter, |
| 11 | EXTRACT(YEAR FROM pmr.Date_Created) AS Year, |
| 12 | COUNT(DISTINCT pmr.RequestID) AS Total_Profile_Requests, |
| 13 | COUNT(DISTINCT pmr.RequestID) FILTER (WHERE pmr.Status = 'Completed') AS Completed_Profile_Requests, |
| 14 | COUNT(DISTINCT pmr.RequestID) FILTER (WHERE pmr.Status = 'In Progress') AS InProgress_Profile_Requests, |
| 15 | COUNT(DISTINCT pmr.RequestID) FILTER (WHERE pmr.Status = 'Pending') AS Pending_Profile_Requests, |
| 16 | SUM(pmr.Target_Followers) AS Total_Target_Followers, |
| 17 | AVG(pmr.Target_Followers) AS Average_Target_Followers, |
| 18 | SUM(pmr.Target_Followers) - SUM(sp.Followers_Count) AS Followers_Gap |
| 19 | |
| 20 | FROM |
| 21 | user u |
| 22 | LEFT JOIN social_media_profile smp ON smp.UserID = u.UserID |
| 23 | LEFT JOIN profile_marketing_request pmr ON pmr.ProfileID = smp.ProfileID |
| 24 | LEFT JOIN service_provider sp ON sp.ProviderID = pmr.ProviderID |
| 25 | WHERE |
| 26 | pmr.Date_Created >= DATE_TRUNC('year', CURRENT_DATE) -- Optional: Filter by the current year |
| 27 | GROUP BY |
| 28 | u.UserID, |
| 29 | u.Username, |
| 30 | EXTRACT(QUARTER FROM pmr.Date_Created), |
| 31 | EXTRACT(YEAR FROM pmr.Date_Created) |
| 32 | ORDER BY |
| 33 | Year DESC, |
| 34 | Quarter DESC; |
| 35 | |
| 36 | }}} |
| 37 | |
| 38 | |
| 39 | == Total Marketing Requests per User == |
| 40 | |
| 41 | This query retrieves the total number of marketing requests submitted by each user. |
| 42 | |
| 43 | {{{ |
| 44 | SELECT |
| 45 | u.user_id, |
| 46 | u.username, |
| 47 | COUNT(DISTINCT pmr.request_id) AS profile_requests, |
| 48 | COUNT(DISTINCT ppr.request_id) AS post_requests, |
| 49 | (COUNT(DISTINCT pmr.request_id) + COUNT(DISTINCT ppr.request_id)) AS total_requests |
| 50 | FROM users u |
| 51 | LEFT JOIN profile_marketing_requests pmr ON u.user_id = pmr.user_id |
| 52 | LEFT JOIN post_marketing_requests ppr ON u.user_id = ppr.user_id |
| 53 | GROUP BY u.user_id, u.username; |
| 54 | |
| 55 | }}} |
| 56 | |
| 57 | == Track Progress of Profile Marketing Request == |
| 58 | This SQL report will track the progress for profile marketing requests and provide a detailed breakdown of the progress made, including the percentage of completion. |
| 59 | |
| 60 | {{{ |
| 61 | SELECT |
| 62 | u.Username AS User_Name, |
| 63 | smp.Platform AS Social_Media_Platform, |
| 64 | pmr.RequestID AS Marketing_Request_ID, |
| 65 | pmr.Target_Followers AS Target_Followers, |
| 66 | smp.Followers_Count AS Current_Followers, |
| 67 | (smp.Followers_Count / pmr.Target_Followers) * 100 AS Progress_Percentage, |
| 68 | pmr.Status AS Request_Status, |
| 69 | pmr.Timeline AS Goal_Timeline, |
| 70 | pmr.Date_Created AS Request_Creation_Date, |
| 71 | DATEDIFF(CURRENT_DATE, pmr.Date_Created) AS Days_Since_Creation, |
| 72 | DATEDIFF(pmr.Timeline, CURRENT_DATE) AS Days_Remaining |
| 73 | FROM |
| 74 | profile_marketing_request pmr |
| 75 | JOIN |
| 76 | social_media_profile smp ON smp.ProfileID = pmr.ProfileID |
| 77 | JOIN |
| 78 | user u ON u.UserID = smp.UserID |
| 79 | WHERE |
| 80 | pmr.Status IN ('In Progress', 'Pending') |
| 81 | ORDER BY |
| 82 | pmr.Date_Created DESC; |
| 83 | |
| 84 | }}} |
| 85 | |
| 86 | == Track Progress of Profile Marketing Request == |
| 87 | This SQL report will track the progress for post marketing requests and provide a detailed breakdown of the progress made, including the percentage of completion. |
| 88 | |
| 89 | {{{ |
| 90 | SELECT |
| 91 | pmr.RequestID, |
| 92 | p.Content AS Post_Content, |
| 93 | p.Likes_Count AS Current_Likes, |
| 94 | p.Comments_Count AS Current_Comments, |
| 95 | pmr.Target_Likes, |
| 96 | pmr.Target_Comments, |
| 97 | -- Calculate percentage of likes and comments achieved |
| 98 | ROUND((p.Likes_Count / pmr.Target_Likes) * 100, 2) AS Likes_Progress_Percentage, |
| 99 | ROUND((p.Comments_Count / pmr.Target_Comments) * 100, 2) AS Comments_Progress_Percentage, |
| 100 | -- Calculate the remaining time in days |
| 101 | DATEDIFF(pmr.Timeline, CURRENT_DATE) AS Days_Remaining, |
| 102 | -- Determine the progress status based on the progress |
| 103 | CASE |
| 104 | WHEN pmr.Status = 'Completed' THEN 'Completed' |
| 105 | WHEN (p.Likes_Count >= pmr.Target_Likes AND p.Comments_Count >= pmr.Target_Comments) THEN 'Completed' |
| 106 | WHEN DATEDIFF(pmr.Timeline, CURRENT_DATE) <= 0 THEN 'Deadline Passed' |
| 107 | ELSE 'In Progress' |
| 108 | END AS Request_Status |
| 109 | FROM |
| 110 | post_marketing_request pmr |
| 111 | JOIN |
| 112 | post p ON pmr.PostID = p.PostID |
| 113 | WHERE |
| 114 | pmr.Status <> 'Completed' -- To show only active or pending requests |
| 115 | ORDER BY |
| 116 | pmr.Date_Created DESC; |
| 117 | |
| 118 | }}} |
| 119 | |
| 120 | |
| 121 | == Procedure: Track Marketing Request Progress == |
| 122 | |
| 123 | {{{ |
| 124 | DELIMITER $$ |
| 125 | |
| 126 | CREATE PROCEDURE TrackMarketingRequestProgress( |
| 127 | IN request_type VARCHAR(50), -- Type of the request ('profile' or 'post') |
| 128 | IN request_id INT |
| 129 | ) |
| 130 | BEGIN |
| 131 | DECLARE target INT; |
| 132 | DECLARE current INT; |
| 133 | DECLARE progress_percentage DECIMAL(5,2); |
| 134 | DECLARE request_status VARCHAR(50); |
| 135 | DECLARE deadline DATE; |
| 136 | DECLARE current_date DATE; |
| 137 | |
| 138 | SET current_date = CURDATE(); -- Get today's date |
| 139 | |
| 140 | -- For profile marketing request |
| 141 | IF request_type = 'profile' THEN |
| 142 | -- Retrieve target followers from profile marketing request |
| 143 | SELECT Target_Followers, Followers_Count, Status, Timeline |
| 144 | INTO target, current, request_status, deadline |
| 145 | FROM profile_marketing_request pmr |
| 146 | JOIN social_media_profile smp ON pmr.ProfileID = smp.ProfileID |
| 147 | WHERE pmr.RequestID = request_id; |
| 148 | |
| 149 | -- Calculate the percentage of the target achieved |
| 150 | SET progress_percentage = (current / target) * 100; |
| 151 | |
| 152 | -- Return the progress report |
| 153 | SELECT |
| 154 | request_id AS RequestID, |
| 155 | target AS TargetFollowers, |
| 156 | current AS CurrentFollowers, |
| 157 | progress_percentage AS ProgressPercentage, |
| 158 | request_status AS Status, |
| 159 | deadline AS Deadline, |
| 160 | DATEDIFF(deadline, current_date) AS DaysRemaining; |
| 161 | |
| 162 | -- For post marketing request |
| 163 | ELSEIF request_type = 'post' THEN |
| 164 | -- Retrieve target likes/comments from post marketing request |
| 165 | SELECT Target_Likes, Target_Comments, Likes_Count, Comments_Count, Status, Timeline |
| 166 | INTO target, current, request_status, deadline |
| 167 | FROM post_marketing_request pmr |
| 168 | JOIN post p ON pmr.PostID = p.PostID |
| 169 | WHERE pmr.RequestID = request_id; |
| 170 | |
| 171 | -- Calculate the percentage of the target achieved |
| 172 | SET progress_percentage = (current / target) * 100; |
| 173 | |
| 174 | -- Return the progress report |
| 175 | SELECT |
| 176 | request_id AS RequestID, |
| 177 | target AS TargetLikesComments, |
| 178 | current AS CurrentLikesComments, |
| 179 | progress_percentage AS ProgressPercentage, |
| 180 | request_status AS Status, |
| 181 | deadline AS Deadline, |
| 182 | DATEDIFF(deadline, current_date) AS DaysRemaining; |
| 183 | END IF; |
| 184 | END $$ |
| 185 | |
| 186 | DELIMITER ; |
| 187 | |
| 188 | }}} |
| 189 | |
| 190 | Usage: |
| 191 | |
| 192 | {{{ |
| 193 | CALL TrackMarketingRequestProgress('profile', 101); |
| 194 | CALL TrackMarketingRequestProgress('post', 202); |
| 195 | }}} |
| 196 | |
| 197 | |
| 198 | == Most Popular Marketing Goal == |
| 199 | This report will provide an analysis of which marketing goal (e.g., increasing followers or increasing likes) is the most popular among users. Since there are two types of marketing requests—Profile Marketing Requests and Post Marketing Requests—we will treat them as separate entities and analyze the frequency of each goal type across both entities. |
| 200 | {{{ |
| 201 | SELECT |
| 202 | CASE |
| 203 | WHEN pmr.Target_Followers IS NOT NULL THEN 'Profile-Based' |
| 204 | ELSE 'Post-Based' |
| 205 | END AS Request_Type, |
| 206 | CASE |
| 207 | WHEN pmr.Target_Followers IS NOT NULL THEN pmr.Target_Followers |
| 208 | ELSE prm.Target_Likes + prm.Target_Comments |
| 209 | END AS Target_Value, |
| 210 | COUNT(*) AS Total_Requests, |
| 211 | AVG(prm.Timeline) AS Avg_Timeline |
| 212 | FROM |
| 213 | profile_marketing_request pmr |
| 214 | LEFT JOIN |
| 215 | post_marketing_request prm ON pmr.RequestID = prm.RequestID |
| 216 | GROUP BY |
| 217 | Request_Type, |
| 218 | CASE |
| 219 | WHEN pmr.Target_Followers IS NOT NULL THEN pmr.Target_Followers |
| 220 | ELSE prm.Target_Likes + prm.Target_Comments |
| 221 | END |
| 222 | ORDER BY |
| 223 | Total_Requests DESC, Target_Value DESC; |
| 224 | |
| 225 | }}} |
| 226 | |
| 227 | |
| 228 | == Get Monthly Marketing Request Statistics == |
| 229 | |
| 230 | This stored procedure retrieves monthly statistics for marketing requests, separating profile-based and post-based requests. |
| 231 | {{{ |
| 232 | DELIMITER // |
| 233 | |
| 234 | CREATE PROCEDURE GetMonthlyMarketingRequestStatistics(IN targetMonth INT, IN targetYear INT) |
| 235 | BEGIN |
| 236 | -- Declare variables to hold the statistics |
| 237 | DECLARE totalRequests INT; |
| 238 | DECLARE totalCompletedRequests INT; |
| 239 | DECLARE avgTimeline DECIMAL(10, 2); |
| 240 | DECLARE totalTargetFollowers INT; |
| 241 | DECLARE totalTargetLikes INT; |
| 242 | DECLARE totalTargetComments INT; |
| 243 | |
| 244 | -- Get the total number of marketing requests in the given month |
| 245 | SELECT COUNT(*) INTO totalRequests |
| 246 | FROM profile_marketing_request |
| 247 | WHERE MONTH(Date_Created) = targetMonth AND YEAR(Date_Created) = targetYear |
| 248 | UNION ALL |
| 249 | SELECT COUNT(*) |
| 250 | FROM post_marketing_request |
| 251 | WHERE MONTH(Date_Created) = targetMonth AND YEAR(Date_Created) = targetYear; |
| 252 | |
| 253 | -- Get the total number of completed requests |
| 254 | SELECT COUNT(*) INTO totalCompletedRequests |
| 255 | FROM profile_marketing_request |
| 256 | WHERE MONTH(Date_Created) = targetMonth AND YEAR(Date_Created) = targetYear AND Status = 'Completed' |
| 257 | UNION ALL |
| 258 | SELECT COUNT(*) |
| 259 | FROM post_marketing_request |
| 260 | WHERE MONTH(Date_Created) = targetMonth AND YEAR(Date_Created) = targetYear AND Status = 'Completed'; |
| 261 | |
| 262 | -- Get the average timeline for marketing requests in the given month |
| 263 | SELECT AVG(DATEDIFF(NOW(), Date_Created)) INTO avgTimeline |
| 264 | FROM profile_marketing_request |
| 265 | WHERE MONTH(Date_Created) = targetMonth AND YEAR(Date_Created) = targetYear |
| 266 | UNION ALL |
| 267 | SELECT AVG(DATEDIFF(NOW(), Date_Created)) |
| 268 | FROM post_marketing_request |
| 269 | WHERE MONTH(Date_Created) = targetMonth AND YEAR(Date_Created) = targetYear; |
| 270 | |
| 271 | -- Get the total target followers requested in the given month |
| 272 | SELECT SUM(Target_Followers) INTO totalTargetFollowers |
| 273 | FROM profile_marketing_request |
| 274 | WHERE MONTH(Date_Created) = targetMonth AND YEAR(Date_Created) = targetYear; |
| 275 | |
| 276 | -- Get the total target likes and comments requested in the given month |
| 277 | SELECT SUM(Target_Likes) INTO totalTargetLikes |
| 278 | FROM post_marketing_request |
| 279 | WHERE MONTH(Date_Created) = targetMonth AND YEAR(Date_Created) = targetYear; |
| 280 | |
| 281 | SELECT SUM(Target_Comments) INTO totalTargetComments |
| 282 | FROM post_marketing_request |
| 283 | WHERE MONTH(Date_Created) = targetMonth AND YEAR(Date_Created) = targetYear; |
| 284 | |
| 285 | -- Output the results |
| 286 | SELECT |
| 287 | totalRequests AS "Total Requests", |
| 288 | totalCompletedRequests AS "Completed Requests", |
| 289 | avgTimeline AS "Average Timeline (Days)", |
| 290 | totalTargetFollowers AS "Total Target Followers", |
| 291 | totalTargetLikes AS "Total Target Likes", |
| 292 | totalTargetComments AS "Total Target Comments"; |
| 293 | |
| 294 | END // |
| 295 | |
| 296 | DELIMITER ; |
| 297 | |
| 298 | }}} |
| 299 | |
| 300 | Usage: |
| 301 | |
| 302 | {{{ |
| 303 | CALL GetMonthlyMarketingRequestStatistics(1, 2025); |
| 304 | }}} |
| 305 | |
| 306 | |