我有以下表格:
location_location
----------------
id
name
email
sales_sale
---------
id
date
total_amount
location_id
client_id
client_client
-------------
id
name
我想创建一个查询,该查询提供Sales列的SUM,并按月,位置和客户名称对结果进行分组,如下所示:
Desired Result:
Location name / month / sum(total_amount) / client_name
San Francisco Jan 250 CISCO
我的尝试:
select name(select name from location_location),
to_char(date,'Mon') as mon,
sum("total_amount") as "total_amount",
client_amount
from sales_sale
group by 1,3
order by mon desc
INNER JOIN name ON client_client.id =sales_sale.client_id and
INNER JOIN location ON location_location.id =sales_sale.location_id;
答案 0 :(得分:1)
虽然你的语法略有偏差,但你还是有了正确的想法:
SELECT l.name,
TO_CHAR(date, 'MON')
client_name,
SUM(total_amount)
FROM location_location l
JOIN sales_sale s ON s.location_id = l.id
JOIN client_client c ON s.client_id = c.id
GROUP BY l.name,
TO_CHAR(date, 'MON')
client_name