MySQL Forums
Forum List  »  Views

Multiple joins and temporay table within one view
Posted by: Magnus Schauer
Date: January 05, 2011 05:55AM

I want to create a view with multiple joins.
There is include one GROUP BY function, and I want to add an additional GROUP BY function.
I only archived my solution with two or three views, but I want it in one view.

Here the views:

delimiter $$

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `user_stats_view` AS
select `c`.`receiver_iduser` AS `receiver_iduser`,count(`c`.`idcommunication`) AS `number_of_rfqs`,count(`q`.`rfq_communication_idcommunication`) AS `number_of_quotes`,(count(`q`.`rfq_communication_idcommunication`) / count(`c`.`idcommunication`)) AS `ratio_of_answered_rfqs`,`u`.`company_idcompany` AS `idcompany`,`comp`.`company_type_idcompany_type` AS `company_type_idcompany_type`
from ((((`communication` `c`
join `rfq` `r` on((`c`.`idcommunication` = `r`.`communication_idcommunication`)))
left join `quote` `q` on((`r`.`communication_idcommunication` = `q`.`rfq_communication_idcommunication`)))
join `user` `u` on((`c`.`receiver_iduser` = `u`.`iduser`)))
join `company` `comp` on((`u`.`company_idcompany` = `comp`.`idcompany`))) group by `c`.`receiver_iduser`$$

view user_stats_view:
receiver_iduser|number_of_rfqs|number_of_quotes|ratio_of_answered_rfqs|idcompany|company_type_idcompany_type

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `rfq_average_view` AS
select `user_stats_view`.`company_type_idcompany_type` AS `company_type_idcompany_type`,avg(`user_stats_view`.`ratio_of_answered_rfqs`) AS `avg_of_ratio_of_answered_rfqs_within_company_type)`
from `user_stats_view` group by `user_stats_view`.`company_type_idcompany_type`$$

view rfq_average_view:
company_type_idcompany_type|avg_of_ratio_of_answered_rfqs_within_company_type

SELECT *
FROM `trunktable`.`user_stats_view` `user_stats`
left join `trunktable`.`rfq_average_view` `rfq_avg` ON `rfq_avg`.`company_type_idcompany_type` = `user_stats`.`company_type_idcompany_type` ;

Select:
receiver_iduser|number_of_rfqs|number_of_quotes|ratio_of_answered_rfqs|idcompany|company_type_idcompany_type|company_type_idcompany_type|avg_of_ratio_of_answered_rfqs_within_company_type

Is there a posibility to have result of the last select already in the first view?

Options: ReplyQuote


Subject
Views
Written By
Posted
Multiple joins and temporay table within one view
3373
January 05, 2011 05:55AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.