File indexing completed on 2024-04-21 06:00:37

0001 
0002 ALTER TABLE `micro_payout`
0003         ADD COLUMN `org_factor` DECIMAL(3,2) NULL DEFAULT '1.00' AFTER `credits_plings`,
0004         ADD COLUMN `credits_org` DECIMAL(11,2) NULL DEFAULT NULL AFTER `org_factor`;
0005 
0006 
0007 
0008 DROP VIEW stat_micro_payout_dl_curent_month;
0009 
0010 CREATE VIEW stat_micro_payout_dl_curent_month AS        
0011 
0012 SELECT 
0013         date_format(`d`.`downloaded_timestamp`,'%Y%m') AS `yearmonth`
0014         ,0 AS `type` 
0015         ,`p`.`project_id` AS `project_id`
0016         ,`p`.`project_category_id` AS `project_category_id`
0017         ,p.ppload_collection_id AS `collection_id`
0018         ,`d`.`file_id`
0019         ,`d`.`owner_id` AS `member_id`
0020         ,`m`.`mail` AS `mail`
0021         ,`m`.`paypal_mail` AS `paypal_mail`
0022         ,count(`d`.`id`) AS `num_plings`
0023         ,`c`.`dl_pling_factor` AS `category_pling_factor`
0024         ,(count(`d`.`id`) * `c`.`dl_pling_factor`) AS `credits_plings`
0025         ,case 
0026      when tag_original.tag_item_id IS NOT NULL 
0027      then 1.0
0028      ELSE 
0029        case 
0030           when tag_modification.tag_item_id IS NOT NULL 
0031           then 0.25
0032        ELSE
0033                         0.1
0034        END  
0035    END AS org_factor
0036         ,round(case 
0037      when tag_original.tag_item_id IS NOT NULL 
0038      then count(`d`.`id`) * `c`.`dl_pling_factor`
0039      ELSE 
0040        case 
0041           when tag_modification.tag_item_id IS NOT NULL 
0042           then count(`d`.`id`) * `c`.`dl_pling_factor`*0.25
0043        ELSE
0044                         count(`d`.`id`) * `c`.`dl_pling_factor`*0.1
0045        END  
0046    END,2) AS credits_org
0047         ,0 as section_id
0048         ,1.00 as section_payout_factor #case when sfs.factor IS NULL then 1.00 ELSE sfs.factor end AS section_payout_factor
0049         ,(count(`d`.`id`) * `c`.`dl_pling_factor`) AS credits_section
0050         ,(case when isnull(`tag`.`tag_item_id`) then 1 else 0 end) AS `is_license_missing`
0051         ,(case when (`c`.`source_required` = 1 AND ((`p`.`source_url` is not null and length(`p`.`source_url`) > 0) OR `p`.`gitlab_project_id` is not null) or `c`.`source_required` = 0) then 0 else 1 end) AS `is_source_missing`
0052         ,`p`.`pling_excluded` AS `is_pling_excluded`
0053         ,`m`.`pling_excluded` AS `is_member_pling_excluded`
0054         #, case when isnull(tag_is_original.tag_item_id) then 0 ELSE 1 END AS is_original
0055         #,case when pc.project_clone_id IS NOT NULL then 1 ELSE 0 END AS `is_clone`
0056         ,NOW() AS `created_at`
0057         ,NULL AS `updated_at`
0058 from ((((`ppload`.`ppload_files_downloaded_unique` `d` 
0059 join `pling`.`member` `m` on(((`m`.`member_id` = `d`.`owner_id`) and (`m`.`is_active` = 1)))) 
0060 join `pling`.`tmp_project_for_micro_payout` `p` on((`p`.`ppload_collection_id` = `d`.`collection_id`))) 
0061 join `pling`.`project_category` `c` on((`c`.`project_category_id` = `p`.`project_category_id`))) 
0062 #left JOIN `pling`.section_category sc ON sc.project_category_id = p.project_category_id
0063 #left JOIN `pling`.section_funding_stats sfs ON sfs.section_id = sc.section_id AND sfs.yearmonth = DATE_FORMAT(`d`.`downloaded_timestamp`,'%Y%m')
0064 left join `pling`.`tag_object` `tag` on(((`tag`.`tag_type_id` = 1) and (`tag`.`tag_group_id` = 7) and (`tag`.`tag_object_id` = `p`.`project_id`)))
0065 #LEFT JOIN pling.tag_object tag_is_original ON tag_is_original.tag_id = 2451 and tag_is_original.tag_group_id=11 and tag_is_original.tag_type_id = 1 and tag_is_original.is_deleted = 0 AND tag_is_original.tag_object_id = `p`.`project_id`
0066 #LEFT JOIN project_clone pc ON pc.project_id = p.project_id AND pc.is_valid = 1 AND pc.project_id_parent IS NOT NULL
0067 LEFT JOIN tag_object tag_original ON tag_original.tag_id = 2451 and tag_original.tag_object_id=p.project_id and tag_original.tag_group_id=11 and tag_original.tag_type_id = 1 and tag_original.is_deleted = 0
0068 LEFT JOIN tag_object tag_modification ON tag_modification.tag_id = 6600 and tag_modification.tag_object_id=p.project_id and tag_modification.tag_group_id=11 and tag_modification.tag_type_id = 1 and tag_modification.is_deleted = 0
0069 ) 
0070 where (`d`.`downloaded_timestamp` >= concat(left(now(),7),'-01 00:00:00')) 
0071 GROUP BY DATE_FORMAT(`d`.`downloaded_timestamp`,'%Y%m'),`d`.`owner_id`,`p`.`project_id`,p.ppload_collection_id,`d`.`file_id`
0072 ;
0073 
0074 
0075 
0076 
0077 DROP VIEW stat_micro_payout_dl_last_month;
0078 CREATE VIEW stat_micro_payout_dl_last_month AS  
0079 
0080 SELECT 
0081         date_format(`d`.`downloaded_timestamp`,'%Y%m') AS `yearmonth`
0082         ,0 AS `type` 
0083         ,`p`.`project_id` AS `project_id`
0084         ,`p`.`project_category_id` AS `project_category_id`
0085         ,p.ppload_collection_id AS `collection_id`
0086         ,`d`.`file_id`
0087         ,`d`.`owner_id` AS `member_id`
0088         ,`m`.`mail` AS `mail`
0089         ,`m`.`paypal_mail` AS `paypal_mail`
0090         ,count(`d`.`id`) AS `num_plings`
0091         ,`c`.`dl_pling_factor` AS `category_pling_factor`
0092         ,(count(`d`.`id`) * `c`.`dl_pling_factor`) AS `credits_plings`
0093         ,case 
0094      when tag_original.tag_item_id IS NOT NULL 
0095      then 1.0
0096      ELSE 
0097        case 
0098           when tag_modification.tag_item_id IS NOT NULL 
0099           then 0.25
0100        ELSE
0101                         0.1
0102        END  
0103    END AS org_factor
0104         ,round(case 
0105      when tag_original.tag_item_id IS NOT NULL 
0106      then count(`d`.`id`) * `c`.`dl_pling_factor`
0107      ELSE 
0108        case 
0109           when tag_modification.tag_item_id IS NOT NULL 
0110           then count(`d`.`id`) * `c`.`dl_pling_factor`*0.25
0111        ELSE
0112                         count(`d`.`id`) * `c`.`dl_pling_factor`*0.1
0113        END  
0114    END,2) AS credits_org
0115         ,0 as section_id
0116         ,1.00 as section_payout_factor #case when sfs.factor IS NULL then 1.00 ELSE sfs.factor end AS section_payout_factor
0117         ,(count(`d`.`id`) * `c`.`dl_pling_factor`) AS credits_section# (count(`d`.`id`) * `c`.`dl_pling_factor` * (case when sfs.factor IS NULL then 1.00 ELSE sfs.factor end)) AS credits_section
0118         ,(case when isnull(`tag`.`tag_item_id`) then 1 else 0 end) AS `is_license_missing`
0119         ,(case when (`c`.`source_required` = 1 AND ((`p`.`source_url` is not null and length(`p`.`source_url`) > 0) OR `p`.`gitlab_project_id` is not null) or `c`.`source_required` = 0) then 0 else 1 end) AS `is_source_missing`
0120         ,`p`.`pling_excluded` AS `is_pling_excluded`
0121         ,`m`.`pling_excluded` AS `is_member_pling_excluded`
0122         #, case when isnull(tag_is_original.tag_item_id) then 0 ELSE 1 END AS is_original
0123         ,NOW() AS `created_at`
0124         ,NULL AS `updated_at`
0125 from ((((`ppload`.`ppload_files_downloaded_unique` `d` 
0126 join `pling`.`member` `m` on(((`m`.`member_id` = `d`.`owner_id`) and (`m`.`is_active` = 1)))) 
0127 join `pling`.`tmp_project_for_micro_payout` `p` on((`p`.`ppload_collection_id` = `d`.`collection_id`))) 
0128 join `pling`.`project_category` `c` on((`c`.`project_category_id` = `p`.`project_category_id`))) 
0129 #left JOIN `pling`.section_category sc ON sc.project_category_id = p.project_category_id
0130 #left JOIN `pling`.section_funding_stats sfs ON sfs.section_id = sc.section_id AND sfs.yearmonth = DATE_FORMAT(`d`.`downloaded_timestamp`,'%Y%m')
0131 left join `pling`.`tag_object` `tag` on(((`tag`.`tag_type_id` = 1) and (`tag`.`tag_group_id` = 7) and (`tag`.`tag_object_id` = `p`.`project_id`)))
0132 #LEFT JOIN pling.tag_object tag_is_original ON tag_is_original.tag_id = 2451 and tag_is_original.tag_group_id=11 and tag_is_original.tag_type_id = 1 and tag_is_original.is_deleted = 0 AND tag_is_original.tag_object_id = `p`.`project_id`
0133 LEFT JOIN tag_object tag_original ON tag_original.tag_id = 2451 and tag_original.tag_object_id=p.project_id and tag_original.tag_group_id=11 and tag_original.tag_type_id = 1 and tag_original.is_deleted = 0
0134 LEFT JOIN tag_object tag_modification ON tag_modification.tag_id = 6600 and tag_modification.tag_object_id=p.project_id and tag_modification.tag_group_id=11 and tag_modification.tag_type_id = 1 and tag_modification.is_deleted = 0
0135 
0136 ) 
0137 where (`d`.`downloaded_timestamp` >= concat(left((now() - interval 1 month),7),'-01 00:00:00')) AND (`d`.`downloaded_timestamp` <= concat(left(now(),7),'-01 00:00:00'))
0138 GROUP BY DATE_FORMAT(`d`.`downloaded_timestamp`,'%Y%m'),`d`.`owner_id`,`p`.`project_id`,p.ppload_collection_id,`d`.`file_id`
0139 ;
0140 
0141 
0142 
0143 #2. Mediaviews
0144 
0145 #CREATE VIEW stat_micro_payout_curent_month AS  
0146 
0147 DROP VIEW stat_micro_payout_mv_curent_month;
0148 CREATE VIEW stat_micro_payout_mv_curent_month AS 
0149 SELECT 
0150         DATE_FORMAT(mv.start_timestamp,'%Y%m') AS `yearmonth`
0151         ,1 AS `type` 
0152         ,`p`.`project_id` AS `project_id`
0153         ,`p`.`project_category_id` AS `project_category_id`
0154         ,p.ppload_collection_id AS `collection_id`
0155         ,`mv`.`file_id`
0156         ,`pr`.`member_id`
0157         ,`m`.`mail` AS `mail`
0158         ,`m`.`paypal_mail` AS `paypal_mail`
0159         ,count(`mv`.media_view_id) AS `num_plings`
0160         ,`c`.`mv_pling_factor` AS `category_pling_factor`
0161         ,(count(`mv`.media_view_id) * `c`.`mv_pling_factor`) AS `credits_plings`
0162         ,case 
0163      when tag_original.tag_item_id IS NOT NULL 
0164      then 1.0
0165      ELSE 
0166        case 
0167           when tag_modification.tag_item_id IS NOT NULL 
0168           then 0.25
0169        ELSE
0170                         0.1
0171        END  
0172    END AS org_factor
0173         ,round(case 
0174      when tag_original.tag_item_id IS NOT NULL 
0175      then count(`mv`.media_view_id) * `c`.`mv_pling_factor`
0176      ELSE 
0177        case 
0178           when tag_modification.tag_item_id IS NOT NULL 
0179           then count(`mv`.media_view_id) * `c`.`mv_pling_factor`*0.25
0180        ELSE
0181                         count(`mv`.media_view_id) * `c`.`mv_pling_factor`*0.1
0182        END  
0183    END,2) AS credits_org
0184         ,0 as section_id
0185         ,1.0 AS section_payout_factor
0186         ,(count(`mv`.media_view_id) * `c`.`mv_pling_factor`) AS credits_section 
0187         ,(case when isnull(`tag`.`tag_item_id`) then 1 else 0 end) AS `is_license_missing`
0188         ,(case when (`c`.`source_required` = 1 AND ((`p`.`source_url` is not null and length(`p`.`source_url`) > 0) OR `p`.`gitlab_project_id` is not null) or `c`.`source_required` = 0) then 0 else 1 end) AS `is_source_missing`
0189         ,`p`.`pling_excluded` AS `is_pling_excluded`
0190         ,`m`.`pling_excluded` AS `is_member_pling_excluded`
0191         #, case when isnull(tag_is_original.tag_item_id) then 0 ELSE 1 END AS is_original
0192         ,NOW() AS `created_at`
0193         ,NULL AS `updated_at`
0194 FROM media_views mv
0195 JOIN project pr ON pr.project_id = mv.project_id
0196 join `pling`.`member` `m` ON `m`.`member_id` = `pr`.`member_id` and `m`.`is_active` = 1
0197 join `pling`.`tmp_project_for_micro_payout` `p` ON `p`.`project_id` = `mv`.`project_id`
0198 join `pling`.`project_category` `c` ON `c`.`project_category_id` = `p`.`project_category_id`
0199 #left JOIN `pling`.section_category sc ON sc.project_category_id = p.project_category_id
0200 #left JOIN `pling`.section_funding_stats sfs ON sfs.section_id = sc.section_id AND sfs.yearmonth = DATE_FORMAT(mv.start_timestamp,'%Y%m')
0201 left join `pling`.`tag_object` `tag` ON (`tag`.`tag_type_id` = 1) and (`tag`.`tag_group_id` = 7) AND (`tag`.`tag_object_id` = `p`.`project_id`)
0202 #LEFT JOIN pling.tag_object tag_is_original ON tag_is_original.tag_id = 2451 and tag_is_original.tag_group_id=11 and tag_is_original.tag_type_id = 1 and tag_is_original.is_deleted = 0 AND tag_is_original.tag_object_id = `p`.`project_id`
0203 LEFT JOIN tag_object tag_original ON tag_original.tag_id = 2451 and tag_original.tag_object_id=pr.project_id and tag_original.tag_group_id=11 and tag_original.tag_type_id = 1 and tag_original.is_deleted = 0
0204 LEFT JOIN tag_object tag_modification ON tag_modification.tag_id = 6600 and tag_modification.tag_object_id=pr.project_id and tag_modification.tag_group_id=11 and tag_modification.tag_type_id = 1 and tag_modification.is_deleted = 0
0205 
0206 WHERE mv.start_timestamp >= concat(left(now(),7),'-01 00:00:00')
0207 GROUP BY DATE_FORMAT(mv.start_timestamp,'%Y%m'),pr.member_id, mv.project_id,`p`.`project_category_id`,p.ppload_collection_id,`mv`.`file_id`
0208 ;
0209 
0210 
0211 DROP VIEW stat_micro_payout_mv_last_month;
0212 CREATE VIEW stat_micro_payout_mv_last_month AS 
0213 SELECT 
0214         DATE_FORMAT(mv.start_timestamp,'%Y%m') AS `yearmonth`
0215         ,1 AS `type` 
0216         ,`p`.`project_id` AS `project_id`
0217         ,`p`.`project_category_id` AS `project_category_id`
0218         ,p.ppload_collection_id AS `collection_id`
0219         ,`mv`.`file_id`
0220         ,`pr`.`member_id`
0221         ,`m`.`mail` AS `mail`
0222         ,`m`.`paypal_mail` AS `paypal_mail`
0223         ,count(`mv`.media_view_id) AS `num_plings`
0224         ,`c`.`mv_pling_factor` AS `category_pling_factor`
0225         ,(count(`mv`.media_view_id) * `c`.`mv_pling_factor`) AS `credits_plings`
0226         ,case 
0227      when tag_original.tag_item_id IS NOT NULL 
0228      then 1.0
0229      ELSE 
0230        case 
0231           when tag_modification.tag_item_id IS NOT NULL 
0232           then 0.25
0233        ELSE
0234                         0.1
0235        END  
0236    END AS org_factor
0237         ,round(case 
0238      when tag_original.tag_item_id IS NOT NULL 
0239      then count(`mv`.media_view_id) * `c`.`mv_pling_factor`
0240      ELSE 
0241        case 
0242           when tag_modification.tag_item_id IS NOT NULL 
0243           then count(`mv`.media_view_id) * `c`.`mv_pling_factor`*0.25
0244        ELSE
0245                         count(`mv`.media_view_id) * `c`.`mv_pling_factor`*0.1
0246        END  
0247    END,2) AS credits_org
0248         ,0 as section_id
0249         ,1.0 AS section_payout_factor
0250         ,(count(`mv`.media_view_id) * `c`.`mv_pling_factor`) AS credits_section 
0251         ,(case when isnull(`tag`.`tag_item_id`) then 1 else 0 end) AS `is_license_missing`
0252         ,(case when (`c`.`source_required` = 1 AND ((`p`.`source_url` is not null and length(`p`.`source_url`) > 0) OR `p`.`gitlab_project_id` is not null) or `c`.`source_required` = 0) then 0 else 1 end) AS `is_source_missing`
0253         ,`p`.`pling_excluded` AS `is_pling_excluded`
0254         ,`m`.`pling_excluded` AS `is_member_pling_excluded`
0255         #, case when isnull(tag_is_original.tag_item_id) then 0 ELSE 1 END AS is_original
0256         ,NOW() AS `created_at`
0257         ,NULL AS `updated_at`
0258 FROM media_views mv
0259 JOIN project pr ON pr.project_id = mv.project_id
0260 join `pling`.`member` `m` ON `m`.`member_id` = `pr`.`member_id` and `m`.`is_active` = 1
0261 join `pling`.`tmp_project_for_micro_payout` `p` ON `p`.`project_id` = `mv`.`project_id`
0262 join `pling`.`project_category` `c` ON `c`.`project_category_id` = `p`.`project_category_id`
0263 #left JOIN `pling`.section_category sc ON sc.project_category_id = p.project_category_id
0264 #left JOIN `pling`.section_funding_stats sfs ON sfs.section_id = sc.section_id AND sfs.yearmonth = DATE_FORMAT(mv.start_timestamp,'%Y%m')
0265 left join `pling`.`tag_object` `tag` ON (`tag`.`tag_type_id` = 1) and (`tag`.`tag_group_id` = 7) AND (`tag`.`tag_object_id` = `p`.`project_id`)
0266 #LEFT JOIN pling.tag_object tag_is_original ON tag_is_original.tag_id = 2451 and tag_is_original.tag_group_id=11 and tag_is_original.tag_type_id = 1 and tag_is_original.is_deleted = 0 AND tag_is_original.tag_object_id = `p`.`project_id`
0267 LEFT JOIN tag_object tag_original ON tag_original.tag_id = 2451 and tag_original.tag_object_id=pr.project_id and tag_original.tag_group_id=11 and tag_original.tag_type_id = 1 and tag_original.is_deleted = 0
0268 LEFT JOIN tag_object tag_modification ON tag_modification.tag_id = 6600 and tag_modification.tag_object_id=pr.project_id and tag_modification.tag_group_id=11 and tag_modification.tag_type_id = 1 and tag_modification.is_deleted = 0
0269 
0270 WHERE (`mv`.start_timestamp >= concat(left((now() - interval 1 month),7),'-01 00:00:00')) and (`mv`.start_timestamp <= concat(left(now(),7),'-01 00:00:00'))
0271 GROUP BY DATE_FORMAT(mv.start_timestamp,'%Y%m'),pr.member_id, mv.project_id,`p`.`project_category_id`,p.ppload_collection_id,`mv`.`file_id`
0272 ;
0273 
0274 
0275 
0276 UPDATE micro_payout m
0277 SET m.credits_org = m.credits_plings
0278 WHERE m.credits_org IS NULL;
0279 
0280 
0281 
0282 
0283 DROP PROCEDURE `generate_section_funding_stats_micro_payout`;
0284 
0285 DELIMITER $$
0286 CREATE PROCEDURE `generate_section_funding_stats_micro_payout`(
0287         IN `p_yearmonth` INT
0288 )
0289 BEGIN
0290 
0291     delete from section_funding_stats where yearmonth = p_yearmonth;
0292 
0293     INSERT INTO section_funding_stats 
0294     SELECT  yearmonth,section_id,section_name
0295                 ,case when sum_affiliate_payout IS NOT NULL then (sum_support-sum_affiliate_payout) ELSE sum_support END AS sum_support
0296                 ,sum_sponsor,sum_dls,sum_amount,sum_dls_payout, sum_amount_payout
0297                 ,case when sum_affiliate_payout IS NOT NULL then ROUND((sum_support-sum_affiliate_payout)/sum_amount_payout,2) ELSE ROUND(sum_support/sum_amount_payout,2) END AS factor 
0298     FROM (        
0299                         SELECT p.yearmonth, p.section_id, se.name AS section_name   
0300                                                                  ,(SELECT ROUND(SUM(ss.tier),2) AS sum_support FROM section_support_paypements ss
0301                                             JOIN support su2 ON su2.id = ss.support_id
0302                                             WHERE p.section_id = ss.section_id
0303                                             AND ss.yearmonth = p_yearmonth 
0304                                             GROUP BY p.section_id
0305                                         ) AS sum_support
0306                                         ,affiliate_payout.sum_affiliate_payout
0307                                         ,(SELECT SUM(sp.amount * (ssp.percent_of_sponsoring/100)) AS sum_sponsor FROM sponsor sp
0308                                         LEFT JOIN section_sponsor ssp ON ssp.sponsor_id = sp.sponsor_id
0309                                         WHERE sp.is_active = 1
0310                                         AND ssp.section_id = p.section_id) AS sum_sponsor
0311                                         , SUM(p.num_plings) AS sum_dls
0312                                         , ROUND(SUM(p.credits_org)/100,2) AS sum_amount
0313                                         , p3.num_downloads AS sum_dls_payout, p3.amount AS sum_amount_payout
0314                                                                  FROM micro_payout p
0315                                         JOIN section se ON se.section_id = p.section_id  
0316                                                                  LEFT JOIN (
0317                                                 SELECT yearmonth, section_id, SUM(num_downloads) AS num_downloads, round(SUM(amount),2) AS amount FROM (
0318                                                         SELECT m.yearmonth, `m`.`member_id`,`m`.`paypal_mail`, `m`.section_id, sum(`m`.`num_plings`) AS `num_downloads`,sum(`m`.`credits_org`)/100 AS `amount` 
0319                                                         from `micro_payout` `m` 
0320                                                         where ((`m`.`yearmonth` = p_yearmonth) 
0321                                                         and (length(`m`.`paypal_mail`) > 0) and (`m`.`paypal_mail` regexp '^[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}$') and (`m`.`is_license_missing` = 0) and (`m`.`is_source_missing` = 0) and (`m`.`is_pling_excluded` = 0) and (`m`.`is_member_pling_excluded` = 0)) 
0322                                                         group by m.yearmonth, `m`.`member_id`,`m`.`paypal_mail`, `m`.section_id
0323                                                         #HAVING sum(`m`.`probably_payout_amount`) >= 1
0324                                                 ) A GROUP BY yearmonth, section_id
0325                                         ) p3 ON p3.yearmonth = p.yearmonth AND p3.section_id = p.section_id                                        
0326                                         LEFT JOIN (SELECT yearmonth, section_id, round(sum_donations * affiliate_percent,2) AS sum_affiliate_payout FROM (
0327                                                                                    SELECT yearmonth, section_id, COUNT(supporter_member_id) AS count_supporters, SUM(sum_donations) AS sum_donations, 
0328                                                                                             (SELECT percent FROM affiliate_config WHERE A2.yearmonth >= active_from  AND A2.yearmonth <= active_until) AS affiliate_percent
0329                                                                                         FROM (
0330                                                                                             SELECT 
0331                                                                                                         ssp2.`yearmonth`, ssp2.section_id, su3.member_id AS supporter_member_id
0332                                                                                                         ,SUM(ssp2.tier) AS sum_donations
0333                                                                                             from section_support_paypements ssp2
0334                                                                                                                           JOIN section_support ss2 ON ss2.section_support_id = ssp2.section_support_id
0335                                                                                                                           JOIN support su3 ON su3.id = ss2.support_id
0336                                                                                                                           JOIN project pr3 ON pr3.project_id = ss2.project_id
0337                                                                                             WHERE
0338                                                                                                 ssp2.`yearmonth` = p_yearmonth
0339                                                                                             GROUP BY ssp2.`yearmonth`, ssp2.section_id, su3.member_id
0340                                                                                                 
0341                                                                                         ) A2
0342                                                                                         GROUP BY A2.yearmonth, A2.section_id
0343                                                                         ) A3) affiliate_payout ON affiliate_payout.yearmonth = p.yearmonth AND affiliate_payout.section_id = p.section_id
0344                           WHERE p.yearmonth = p_yearmonth
0345                 AND p.section_id IS NOT null
0346                 GROUP BY p.section_id
0347         ) AA
0348         ;
0349 END$$
0350 DELIMITER ;
0351 
0352