MySQL - 每天前10名

时间:2016-08-02 13:18:04

标签: mysql

我需要按PRESENCE&从表中LECTURE DURATION

我尝试了一些事情,但没有运气。这是现在的查询。

我可以请一些指导来做到这一点吗?

select 
        t1.EVE_DATE, 
        t1.CUSTID,
        SUM(t1.ATTENDENCE) as PRESENCE,
        TRUNCATE((AVG(t1.LECTURE_DUR))/(1000),2) as LECTURE_DUR
from 
        MY_TABLE t1
            left join
        (
            select
                CUSTID
            from
                MY_TABLE t2
            where
                t1.EVE_DATE = t2.EVE_DATE
            order by
                t2.CUSTID
            limit 10
        ) t3
            on
        t1.CUSTID = t3.CUSTID
where
        t1.SUBJECT= 'PHYSICS' 
        and t1.EVE_DATE >= '2015-01-01'
        and t1.EVE_DATE <= '2016-01-01'
        and t1.CUSTID <> ''
group by
        t1.EVE_DATE,
        t1.CUSTID
order by
        t1.EVE_DATE

1 个答案:

答案 0 :(得分:1)

假设您希望为每位客户提供10个最新的eve_Dates。

未经测试我认为相关子查询会为每个客户生成结果集,但我对此并不乐观。

我不认为你限制了10条记录,因为你没有参加约会。

select 
        t1.EVE_DATE, 
        t1.CUSTID,
        SUM(t1.ATTENDENCE) as PRESENCE,
        TRUNCATE((AVG(t1.LECTURE_DUR))/(1000),2) as LECTURE_DUR
from 
        MY_TABLE t1
            left join
        (
            select
                CUSTID, eve_date
            from
                MY_TABLE t2
            where
                t1.EVE_DATE = t2.EVE_DATE
            order by
                t2.CUSTID, t2.eve_Date desc
            limit 10
        ) t3
            on
        t1.CUSTID = t3.CUSTID
        t1.Eve_Date = T3.Eve_Date
where
        t1.SUBJECT= 'PHYSICS' 
        and t1.EVE_DATE >= '2015-01-01'
        and t1.EVE_DATE <= '2016-01-01'
        and t1.CUSTID <> ''
group by
        t1.EVE_DATE,
        t1.CUSTID
order by
        t1.EVE_DATE