首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQL -每天前10名

MySQL -每天前10名
EN

Stack Overflow用户
提问于 2016-08-02 13:18:03
回答 1查看 149关注 0票数 0

我需要从一个表中生成PRESENCE & LECTURE DURATION的前十名。

我试过一些东西,但没有运气。下面是现在的查询。

我能得到一些指导才能把这事做好吗?

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-02 13:34:21

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

未测试的--我认为相关子查询将为每个客户生成一个结果集,但我对此并不肯定。

我不认为你限制在10张唱片是因为你没有参加约会。

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38721557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档