过滤掉多个评级的第一个评级

时间:2013-09-25 13:13:03

标签: mysql sql

我为一家提供支持的公司工作,我被要求了解如果用户在其票证历史记录中多次看到相同的自动回复,客户满意度评分是否会下降。到目前为止,我已经拉出了多次看到相同自动回复或“宏”的用户,宏的名称,他们在票证历史中看到的次数,以及评级和评级:

SELECT user_name,  macro_name, COUNT(*), rating
FROM macros_used m
JOIN cases c
ON c.case_number = m.case_number
join ratings r
on r.case_number = c.case_number
where user_name <> ''
GROUP BY user_name, macro_name
HAVING COUNT(macro_name) > 1

我不确定我是否正确地提取评级,我可能需要在案例历史和日期中提取所有案例以获得每个案例的评级。

我的问题是,有没有办法过滤掉用户第一次看到自第一次使用宏以来提供的第一个评级,它不会重复。我试图查看用户评级是否由于重复的宏而下降。

1 个答案:

答案 0 :(得分:0)

您可以使用子查询查找具有该特定宏的案例的第一个和最后一个评级:

select  cm.*
,       first_rating.rating as first_rating
,       last_rating.rating as last_rating
from    (
        select  c.user_name
        ,       m.macro_name
        ,       count(*) as macro_usage_count
        from    cases c
        join    macros_used m
        on      m.fk_case_number = c.pk_case_number
        group by 
                c.user_name
        ,       m.macro_name
        having  count(m.macro_name) > 1
        ) cm
join    (
        select  r.rating
        from    cases c2
        join    macros_used m2
        on      m2.fk_case_number = c2.pk_case_number
                and c2.user_name = cm.user_name
                and m2.macro_name= cm.macro_name
        join    ratings r2
        on      r2.pk_case_number = c2.case_number
        order by
                r.date_submitted
        limit   1
        ) first_rating
join    (
        select  r.rating
        from    cases c2
        join    macros_used m2
        on      m2.fk_case_number = c2.pk_case_number
                and c2.user_name = cm.user_name
                and m2.macro_name= cm.macro_name
        join    ratings r2
        on      r2.pk_case_number = c2.case_number
        order by
                r.date_submitted desc
        limit   1
        ) last_rating
where   m.user_name <> ''