四个表上的SQL JOIN,GROUP BY获取按月记录

时间:2018-02-08 05:22:08

标签: mysql sql database join left-join

我有以下数据库设计。表格是:

auth_user
----------
first_name
last_name

staffuser
----------
phone_number
user_id

billing_customerservicebill
-----------------------------
bill_id
service_provider_id
discounted_price

billing_billmanagement
------------------------
creation_date

我的查询按行逐行返回每个用户的discounted_price总和。我需要每个月的专栏记录。 以下查询给出了此记录

select a.service_provider_id, first_name, Sum(a.discounted_price), EXTRACT(MONTH FROM c.creation_date)
    from billing_customerservicebill a
         left outer join users_staffuser b
         on a.service_provider_id = b.id
         left outer join billing_billmanagement c
         on a.bill_id = c.id
         left outer join auth_user d 
         on d.id = b.user_id
    where c.creation_date between '2017-11-01' AND '2017-12-31'
    group by service_provider_id, first_name, EXTRACT(MONTH FROM c.creation_date)
    order by 1

My data show in Table Currently 
    service_provider_id | first_name | Sum     |  Month
      5                 |   suneel     31500   |  11
      5                 |   Suneel   | 900     |  12

Expected data is 
    service_provider_id | first_name | Nov     |  December
      5                 |   suneel   |  31500  |  900

2 个答案:

答案 0 :(得分:2)

最灵活的方法是使用条件聚合......

select
    a.service_provider_id,
    first_name,
    SUM(CASE WHEN c.creation_date >= '2017-11-01' AND c.creation_date < '2017-12-01' THEN a.discounted_price END)   AS nov,
    SUM(CASE WHEN c.creation_date >= '2017-12-01' AND c.creation_date < '2018-01-01' THEN a.discounted_price END)   AS dec
from billing_customerservicebill a
     left outer join users_staffuser b
     on a.service_provider_id = b.id
     left outer join billing_billmanagement c
     on a.bill_id = c.id
     left outer join auth_user d 
     on d.id = b.user_id
where c.creation_date between '2017-11-01' AND '2017-12-31'
group by service_provider_id, first_name
order by 1

这表明您需要事先知道要计算哪些列。

答案 1 :(得分:0)

请尝试使用以下解决方案,它接近您的答案: 用户所在的月份为列和组:

select  B.service_provider_id, B.first_name, 
(case when month=1 then discounted_price else 0 end) as JAN, 
(case when month=2 then discounted_price else 0 end) as FEB, 
(case when month=3 then discounted_price else 0 end) as MAR, 
(case when month=4 then discounted_price else 0 end) as APR, 
(case when month=5 then discounted_price else 0 end) as MAY, 
(case when month=6 then discounted_price else 0 end) as JUN, 
(case when month=7 then discounted_price else 0 end) as JULY, 
(case when month=8 then discounted_price else 0 end) as AUG, 
(case when month=9 then discounted_price else 0 end) as SEP, 
(case when month=10 then discounted_price else 0 end) as OCT, 
(case when month=11 then discounted_price else 0 end) as NOV, 
(case when month=12 then discounted_price else 0 end) as DEC 
from(
select a.service_provider_id, first_name, Sum(a.discounted_price) as discounted_price, EXTRACT(MONTH FROM c.creation_date) as month
    from billing_customerservicebill a
         left outer join users_staffuser b
         on a.service_provider_id = b.id
         left outer join billing_billmanagement c
         on a.bill_id = c.id
         left outer join auth_user d 
         on d.id = b.user_id
    where c.creation_date between '2017-11-01' AND '2017-12-31'
    group by service_provider_id, first_name, EXTRACT(MONTH FROM c.creation_date)    
) as B
group by B.service_provider_id, B.first_name