Input: 我有一张桌子,里面有一个帐户清单以及他们每天花的钱 . 为简单起见,我们会说该表包含以下字段:account,account_id,date,sales .

Output: 最终目标是获得前25个帐户的报告,并列出前8周的销售情况 . 该报告将包含帐户,account_id,然后为前8周中的每一个提供8个附加列,每列中的销售额相加 .

Problem 1: 我为每个帐户销售'm having trouble grabbing the Top 25 accounts. I wrote a subquery to sum the prior month'并使用带有in运算符的where子句获取前25名,但子查询在运算符中不支持 .

Problem 2: 我不确定如何将前8周转入列 .

select
        yearweek(date,1) as year_week
        ,date_add(makedate(year(date),1), interval week(date,1) week) as first_day_of_week
        ,account_id
        ,account
        ,sum(sales) as total_sales
from daily_sales
where date >= date_sub(curdate(), interval 8 week)
and account_id in (  select
                                    account_id
                                from daily_sales
                                where year(date)=2018 and month(date)=11
                                group by 1
                                limit 25)
group by 1,2,3,4;