
-- --------------------------------------------------------

--
-- Structure for view `days_used_of_trials`
--

CREATE ALGORITHM=UNDEFINED DEFINER=`raimonds`@`%` SQL SECURITY DEFINER VIEW `days_used_of_trials`  AS  select `p`.`id` AS `id`,`p`.`user_id` AS `user_id`,`p`.`plan` AS `plan`,`u`.`company` AS `company`,cast(`p`.`date_from` as date) AS `date_from`,`p`.`date_till` AS `date_till`,if((`p`.`date_till` > now()),'ACTIVE','ended') AS `status`,`p`.`max_days` AS `included_days`,count(`ad`.`id`) AS `days_used` from ((`periods` `p` left join `users` `u` on((`u`.`id` = `p`.`user_id`))) left join `active_days` `ad` on((`ad`.`period_id` = `p`.`id`))) where (`p`.`plan` = 'trial') group by `p`.`id`,`p`.`user_id`,`p`.`plan`,`u`.`company`,cast(`p`.`date_from` as date),`p`.`date_till`,if((`p`.`date_till` > now()),'ACTIVE','ended'),`p`.`max_days` order by `p`.`date_till` desc ;
