select d.user_id as "角色ID"
                        ,c.character_pay_status as "角色是否付费"
                        ,a.pay_amount as "购课金额"
                        ,d.chapter_id as "课程章节"
                        ,d.play_status as "是否完成"
                        ,d.started_at as "开始时间"
                        ,d.finished_at as "结束时间"
                        ,b.created_at as "账号注册时间"
                        ,a.pay_success_date as "购课时间"
from
(
        select account_id
                                ,to_char(pay_success_date,'YYYY-MM-DD') as pay_success_date
                                ,pay_amount
        from bi_vala_order
        where order_status = 3
        --and key_from = 'app-active-h5-0-0'
        and sale_channel in (11,12,13,14,15,16,17,18,19,21,22,23,24,25,26,27,41,71)
        and pay_amount_int > 49800
        group by account_id
                                        ,to_char(pay_success_date,'YYYY-MM-DD')
                                        ,pay_amount
) as a 
left join 
(
        select id
                                ,to_char(created_at,'YYYY-MM-DD') as created_at
        from bi_vala_app_account
        where status = 1
        and id not in (2121,51,1386,1397)
        group by id
                                        ,created_at
) as b on a.account_id = b.id
left join 
(
        select id
                                ,account_id
                                ,case when purchase_season_package = '[1]' then 0
                                                        else 1
                                 end as character_pay_status
        from bi_vala_app_character
        group by id
                                        ,account_id
                                        ,case when purchase_season_package = '[1]' then 0
                                                        else 1
                                         end
) as c on b.id = c.account_id
left join 
(
        select user_id
                                ,case when chapter_id = 55 then '第一节课'
                                                        when chapter_id = 56 then '第二节课'
                                                        when chapter_id = 57 then '第三节课'
                                                        when chapter_id = 58 then '第四节课'
                                                        when chapter_id = 59 then '第五节课'
                                                        end as chapter_id
                                ,to_char(created_at,'YYYY-MM-DD') as started_at
                                ,to_char(created_at,'YYYY-MM-DD') as finished_at
                                ,play_status
        from
        (
                select *
                from bi_user_chapter_play_record_0
                union all
                select *
                from bi_user_chapter_play_record_1
                union all
                select *
                from bi_user_chapter_play_record_2
                union all
                select *
                from bi_user_chapter_play_record_3
                union all
                select *
                from bi_user_chapter_play_record_4
                union all
                select *
                from bi_user_chapter_play_record_5
                union all
                select *
                from bi_user_chapter_play_record_6
                union all
                select *
                from bi_user_chapter_play_record_7
        )
        where chapter_id in (55,56,57,58,59)
        group by user_id
                                        ,case when chapter_id = 55 then '第一节课'
                                                                when chapter_id = 56 then '第二节课'
                                                                when chapter_id = 57 then '第三节课'
                                                                when chapter_id = 58 then '第四节课'
                                                                when chapter_id = 59 then '第五节课'
                                         end
                                        ,to_char(created_at,'YYYY-MM-DD')
                                        ,to_char(created_at,'YYYY-MM-DD')
                                        ,play_status
        ) as d on c.id = d.user_id
where c.character_pay_status = 1 and c.id = 14607
group by a.pay_amount
                                ,d.user_id
                                ,c.character_pay_status
                                ,d.chapter_id
                                ,d.play_status
                                ,d.started_at
                                ,d.finished_at
                                ,b.created_at
                                ,a.pay_success_date
order by d.user_id,d.started_at