CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `imnuu_payments_v` AS select `inv`.`gift_id` AS `gift_id`,`inv`.`v_user_id` AS `v_user_id`,sum(ifnull(`i`.`price`,0)) AS `price`,(sum(ifnull(`i`.`price`,0)) / (select count(distinct `inv2`.`v_user_id`) from (`imnuu_invitation` `inv2` left join `imnuu_organization` `o2` on((`o2`.`invitation_id` = `inv2`.`id`))) where ((`inv2`.`gift_id` = `inv`.`gift_id`) and (`inv2`.`confirmed_at` is not null)))) AS `debit`,(sum(ifnull(`i`.`price`,0)) - (sum(ifnull(`i`.`price`,0)) / (select count(distinct `inv2`.`v_user_id`) from (`imnuu_invitation` `inv2` left join `imnuu_organization` `o2` on((`o2`.`invitation_id` = `inv2`.`id`))) where ((`inv2`.`gift_id` = `inv`.`gift_id`) and (`inv2`.`confirmed_at` is not null))))) AS `credit` from ((`imnuu_invitation` `inv` left join `imnuu_organization` `o` on((`o`.`invitation_id` = `inv`.`id`))) left join `imnuu_idea` `i` on((`o`.`idea_id` = `i`.`id`))) where (`inv`.`confirmed_at` is not null) group by `inv`.`gift_id`,`inv`.`v_user_id` order by `inv`.`gift_id`,`inv`.`v_user_id`;