Please describe your data model with the DDL for the tables and some sample data.
Also describe what you are trying to achieve and what the problem is. For instance, saying the "query doesn't run" is not really helpful because we don't know what error (if any) is thrown by the system.