Fetch last 3 month data as separate columns not rows for each user

前端 未结 3 1978
时光取名叫无心
时光取名叫无心 2021-01-27 00:48

i have a mysql table \"bf_monthly_bill\" in which data against each users from another table \"bf_users\" is stored month wise.

Structure of my bf_users table is

<
3条回答
  •  粉色の甜心
    2021-01-27 01:08

    You can try using conditional aggregation

    select display_name,
           sum(case when bill_month='Jan2k19' then package_price end) as first_bill,
           sum(case when bill_month='Feb2k19' then package_price end) as second_bill,
           sum(case when bill_month='Mar2k19' then package_price end) as third_bill
    from
    (
       SELECT u.display_name,b.package_price,b.bill_month
       from bf_users u inner join bf_monthly_bill b on u.id=b.company_id
    )A group by display_name
    

提交回复
热议问题