我继承了以下数据库设计。表格是:
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总和。结果不准确 以下查询为我提供了正确的值,但first_name的前面不正确:
select a.service_provider_id, first_name, sum(a.discounted_price) As total
from billing_customerservicebill a,
users_staffuser b,
billing_billmanagement c,
auth_user d
where a.service_provider_id = b.id
AND d.id = b.id
AND a.bill_id = c.id
AND c.creation_date between '2017-12-01' AND '2017-12-31'
group by first_name, service_provider_id
order by 1
My data show in Table Currently
service_provider_id | first_name | total
5 suneel 500
8 Ali 900
Expected data is
service_provider_id | first_name | total
5 suneel 1000
8 Ali 500
答案 0 :(得分:1)
@MatBailie这里是我的查询。
select a.service_provider_id, first_name, Sum(a.discounted_price)
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-12-01' AND '2017-12-31'
group by service_provider_id, first_name
order by 1
答案 1 :(得分:-1)
b.id = d.id和service_provider_id = b.id. 是auth_user id = service_provider_id? 你应该重新检查吗?
如果表连接正确,请尝试执行并检查数字:
{{1}}
当你完成并确定字段中的数字然后总结