DROP VIEW IF EXISTS editors_view_es;
CREATE VIEW editors_view_es(
    id,
    user_id,
    email,
    first_last_name,
    user_status,
    cnt_tests,
    avg_results,
    userLanguage,
    userLanguageCode,
    alias,
    reviserPuntualityRate,
    reviserAttitudeRate,
    reviserQualityRate,
    reviserTotalRate,
    reviserTotalArticleScoring,
    editorPuntualityRate,
    editorAttitudeRate,
    editorQualityRate,
    editorTotalRate,
    editorTotalArticleScoring,
    pmPuntualityRate,
    pmAttitudeRate,
    pmQualityRate,
    pmTotalRate,
    pmTotalArticleScoring
) as
SELECT
    editors_view.id,
    editors_view.user_id,
    editors_view.email,
    editors_view.first_last_name,
    editors_view.user_status,
    editors_view.cnt_tests,
    editors_view.avg_results,
    languages.language AS userLanguage,
    languages.language_code AS userLanguageCode,
    user.alias,
    COALESCE(
        reviser_puntuality_rate.rate,
        0
    ) AS reviserPuntualityRate,
    COALESCE(reviser_attitude_rate.rate, 0) AS reviserAttitudeRate,
    COALESCE(reviser_quality_rate.rate, 0) AS reviserQualityRate,
    (
        COALESCE(
            reviser_puntuality_rate.rate,
            0
        ) + COALESCE(reviser_attitude_rate.rate, 0) + COALESCE(reviser_quality_rate.rate, 0)
    ) AS reviserTotalRate,
    reviser_total_article_view.reviserTotalArticleScoring,
    COALESCE(editor_puntuality_rate.rate, 0) AS editorPuntualityRate,
    COALESCE(editor_attitude_rate.rate, 0) AS editorAttitudeRate,
    COALESCE(editor_quality_rate.rate, 0) AS editorQualityRate,
    (
        COALESCE(editor_puntuality_rate.rate, 0) + COALESCE(editor_attitude_rate.rate, 0) + COALESCE(editor_quality_rate.rate, 0)
    ) AS editorTotalRate,
    editor_total_article_view.editorTotalArticleScoring,
    COALESCE(pm_puntuality_rate.rate, 0) AS pmPuntualityRate,
    COALESCE(pm_attitude_rate.rate, 0) AS pmAttitudeRate,
    COALESCE(pm_quality_rate.rate, 0) AS pmQualityRate,
    (
        COALESCE(pm_puntuality_rate.rate, 0) + COALESCE(pm_attitude_rate.rate, 0) + COALESCE(pm_quality_rate.rate, 0)
    ) AS pmTotalRate,
    pm_total_article_view.pmTotalArticleScoring
FROM
    editors_view
LEFT JOIN user ON editors_view.user_id = user.id
LEFT JOIN user_lang ON user.id = user_lang.user_id
LEFT JOIN languages ON user_lang.lang_id = languages.id
LEFT JOIN (
    select
            scoring_amount.user_id AS user_id,
            scoring_amount.participant_type AS participant_type,
            (scoring_amount.total_scoring / scoring_count.count_scoring) AS rate
        from ((
            (
                select sc.user_id, sc.participant_type, sc.scoring_type, sc.total_scoring 
                    from sum_scoring_puntuality_rate_view as sc
                    
            )
            )
            scoring_amount join (
                                select sc.user_id, sc.participant_type, sc.scoring_type, sc.count_scoring 
                                from count_scoring_puntuality_rate_view as sc
                            )
            scoring_count on(
                                    (
                                        (scoring_amount.user_id = scoring_count.user_id) 
                                        and (scoring_amount.participant_type = scoring_count.participant_type) 
                                        and (scoring_amount.scoring_type = scoring_count.scoring_type)
                                    )
                                )
            )
            where scoring_amount.scoring_type = 1
) as reviser_puntuality_rate ON (editors_view.user_id = reviser_puntuality_rate.user_id) AND (reviser_puntuality_rate.participant_type = 6)
LEFT JOIN (
    select 
            scoring_amount.user_id AS user_id,
            scoring_amount.participant_type AS participant_type,
            (scoring_amount.total_scoring / scoring_count.count_scoring) AS rate
        from ((
            (
                select sc.user_id ,sc.participant_type ,sc.scoring_type, sc.total_scoring 
                    from sum_scoring_attitude_rate_view	as sc
            )
            )
            scoring_amount join (
                                select sc.user_id, sc.participant_type, sc.scoring_type, sc.count_scoring 
                                from count_scoring_attitude_rate_view sc
                            )
            scoring_count on(
                                    (
                                        (scoring_amount.user_id = scoring_count.user_id) 
                                        and (scoring_amount.participant_type = scoring_count.participant_type) 
                                        and (scoring_amount.scoring_type = scoring_count.scoring_type)
                                    )
                                )
            )
        where scoring_amount.scoring_type = 2
) as reviser_attitude_rate ON (editors_view.user_id = reviser_attitude_rate.user_id) AND (reviser_attitude_rate.participant_type = 6)
LEFT JOIN (
        select 
            scoring_amount.user_id AS user_id,
            scoring_amount.participant_type AS participant_type,
            (scoring_amount.total_scoring / scoring_count.count_scoring) AS rate
        from ((
            (
                select sc.user_id ,sc.participant_type ,sc.scoring_type, sc.total_scoring 
                    from sum_scoring_quality_rate_view sc 
                    
            )
            )
            scoring_amount join (
                                select sc.user_id, sc.participant_type, sc.scoring_type, sc.count_scoring 
                                from count_scoring_quality_rate_view sc
                            )
            scoring_count on(
                                    (
                                        (scoring_amount.user_id = scoring_count.user_id) 
                                        and (scoring_amount.participant_type = scoring_count.participant_type) 
                                        and (scoring_amount.scoring_type = scoring_count.scoring_type)
                                    )
                                )
            )
        where scoring_amount.scoring_type = 3
) as reviser_quality_rate ON (editors_view.user_id = reviser_quality_rate.user_id) AND (reviser_quality_rate.participant_type = 6)
LEFT JOIN (
    select
            scoring_amount.user_id AS user_id,
            scoring_amount.participant_type AS participant_type,
            (scoring_amount.total_scoring / scoring_count.count_scoring) AS rate
        from ((
            (
                select sc.user_id, sc.participant_type, sc.scoring_type, sc.total_scoring 
                    from sum_scoring_puntuality_rate_view as sc
                    
            )
            )
            scoring_amount join (
                                select sc.user_id, sc.participant_type, sc.scoring_type, sc.count_scoring 
                                from count_scoring_puntuality_rate_view as sc
                            )
            scoring_count on(
                                    (
                                        (scoring_amount.user_id = scoring_count.user_id) 
                                        and (scoring_amount.participant_type = scoring_count.participant_type) 
                                        and (scoring_amount.scoring_type = scoring_count.scoring_type)
                                    )
                                )
            )
            where scoring_amount.scoring_type = 1
) as editor_puntuality_rate ON (editors_view.user_id = editor_puntuality_rate.user_id) AND (editor_puntuality_rate.participant_type = 2)
LEFT JOIN (
     select 
            scoring_amount.user_id AS user_id,
            scoring_amount.participant_type AS participant_type,
            (scoring_amount.total_scoring / scoring_count.count_scoring) AS rate
        from ((
            (
                select sc.user_id ,sc.participant_type ,sc.scoring_type, sc.total_scoring 
                    from sum_scoring_attitude_rate_view	as sc
            )
            )
            scoring_amount join (
                                select sc.user_id, sc.participant_type, sc.scoring_type, sc.count_scoring 
                                from count_scoring_attitude_rate_view sc
                            )
            scoring_count on(
                                    (
                                        (scoring_amount.user_id = scoring_count.user_id) 
                                        and (scoring_amount.participant_type = scoring_count.participant_type) 
                                        and (scoring_amount.scoring_type = scoring_count.scoring_type)
                                    )
                                )
            )
        where scoring_amount.scoring_type = 2
) as editor_attitude_rate ON (editors_view.user_id = editor_attitude_rate.user_id) AND (editor_attitude_rate.participant_type = 2)
LEFT JOIN (
    select 
            scoring_amount.user_id AS user_id,
            scoring_amount.participant_type AS participant_type,
            (scoring_amount.total_scoring / scoring_count.count_scoring) AS rate
        from ((
            (
                select sc.user_id ,sc.participant_type ,sc.scoring_type, sc.total_scoring 
                    from sum_scoring_quality_rate_view sc 
                    
            )
            )
            scoring_amount join (
                                select sc.user_id, sc.participant_type, sc.scoring_type, sc.count_scoring 
                                from count_scoring_quality_rate_view sc
                            )
            scoring_count on(
                                    (
                                        (scoring_amount.user_id = scoring_count.user_id) 
                                        and (scoring_amount.participant_type = scoring_count.participant_type) 
                                        and (scoring_amount.scoring_type = scoring_count.scoring_type)
                                    )
                                )
            )
        where scoring_amount.scoring_type = 3
) as editor_quality_rate ON (editors_view.user_id = editor_quality_rate.user_id) AND (editor_quality_rate.participant_type = 2)
LEFT JOIN (
    select
            scoring_amount.user_id AS user_id,
            scoring_amount.participant_type AS participant_type,
            (scoring_amount.total_scoring / scoring_count.count_scoring) AS rate
        from ((
            (
                select sc.user_id, sc.participant_type, sc.scoring_type, sc.total_scoring 
                    from sum_scoring_puntuality_rate_view as sc
                    
            )
            )
            scoring_amount join (
                                select sc.user_id, sc.participant_type, sc.scoring_type, sc.count_scoring 
                                from count_scoring_puntuality_rate_view as sc
                            )
            scoring_count on(
                                    (
                                        (scoring_amount.user_id = scoring_count.user_id) 
                                        and (scoring_amount.participant_type = scoring_count.participant_type) 
                                        and (scoring_amount.scoring_type = scoring_count.scoring_type)
                                    )
                                )
            )
            where scoring_amount.scoring_type = 1
) as pm_puntuality_rate ON (editors_view.user_id = pm_puntuality_rate.user_id) AND (pm_puntuality_rate.participant_type = 5)
LEFT JOIN (
     select 
            scoring_amount.user_id AS user_id,
            scoring_amount.participant_type AS participant_type,
            (scoring_amount.total_scoring / scoring_count.count_scoring) AS rate
        from ((
            (
                select sc.user_id ,sc.participant_type ,sc.scoring_type, sc.total_scoring 
                    from sum_scoring_attitude_rate_view	as sc
            )
            )
            scoring_amount join (
                                select sc.user_id, sc.participant_type, sc.scoring_type, sc.count_scoring 
                                from count_scoring_attitude_rate_view sc
                            )
            scoring_count on(
                                    (
                                        (scoring_amount.user_id = scoring_count.user_id) 
                                        and (scoring_amount.participant_type = scoring_count.participant_type) 
                                        and (scoring_amount.scoring_type = scoring_count.scoring_type)
                                    )
                                )
            )
        where scoring_amount.scoring_type = 2
) as pm_attitude_rate ON (editors_view.user_id = pm_attitude_rate.user_id) AND (pm_attitude_rate.participant_type = 5)
LEFT JOIN (
    select 
            scoring_amount.user_id AS user_id,
            scoring_amount.participant_type AS participant_type,
            (scoring_amount.total_scoring / scoring_count.count_scoring) AS rate
        from ((
            (
                select sc.user_id ,sc.participant_type ,sc.scoring_type, sc.total_scoring 
                    from sum_scoring_quality_rate_view sc 
                    
            )
            )
            scoring_amount join (
                                select sc.user_id, sc.participant_type, sc.scoring_type, sc.count_scoring 
                                from count_scoring_quality_rate_view sc
                            )
            scoring_count on(
                                    (
                                        (scoring_amount.user_id = scoring_count.user_id) 
                                        and (scoring_amount.participant_type = scoring_count.participant_type) 
                                        and (scoring_amount.scoring_type = scoring_count.scoring_type)
                                    )
                                )
            )
        where scoring_amount.scoring_type = 3
) as pm_quality_rate ON (editors_view.user_id = pm_quality_rate.user_id) AND (pm_quality_rate.participant_type = 5)
LEFT JOIN (
    SELECT
        count(*) as reviserTotalArticleScoring,
        scoring.user_id as user_id
    FROM
        scoring
    WHERE
        scoring.participant_type = 6 AND scoring.scoring_type = 1 AND scoring.scoring > 0
    GROUP BY
        scoring.user_id
) as reviser_total_article_view ON reviser_total_article_view.user_id = user.id
LEFT JOIN (
    SELECT
        count(*) as editorTotalArticleScoring,
        scoring.user_id as user_id
    FROM
        scoring
    WHERE
        scoring.participant_type = 2 AND scoring.scoring_type = 1 AND scoring.scoring > 0
    GROUP BY
        scoring.user_id
) as editor_total_article_view ON editor_total_article_view.user_id = user.id
LEFT JOIN (
    SELECT
        count(*) as pmTotalArticleScoring,
        scoring.user_id as user_id
    FROM
        scoring
    WHERE
        scoring.participant_type = 5 AND scoring.scoring_type = 1 AND scoring.scoring > 0
    GROUP BY
        scoring.user_id
) as pm_total_article_view ON pm_total_article_view.user_id = user.id
ORDER BY
    editorTotalRate
DESC
    ,
    editorTotalArticleScoring
DESC;