I have a sql query to select the sum of amounts as per below scenario.
There are managers who look after sales of many locations. Sale amounts of all managers of all locations are stored in the same table. I want to select the Manager name, sum of amounts of locations and his total amount from overall table.
Following is the SQL which I have. It is working. But looking for more efficient way of implementation considering 100k records in the table.
SELECT
D1.Manager_name,
D1.location_name,
sum(D1.sale_amount) sale_amount,
(select sum(D2.sale_amount) from details D2 where D1.Manager_name = D2.Manager_name) total_amount
FROM details D1
GROUP BY
D1.Manager_name,
D1.location_name;
Table data and expected output data
