I want to do something Like this :
Current table :
Name | coupon_price |
---|---|
John | 10 |
Sammy | 20 |
Ben | 100 |
Joseph | 120 |
John | 90 |
Joseph | 20 |
John | 50 |
Sammy | 30 |
while selecting data in query (Sum of Coupon price for each customer) without loosing the actual rows of customers, i want to have this expected result :
Name | coupon_price | total_coupon_amount |
---|---|---|
John | 10 | 150 |
Sammy | 20 | 50 |
Ben | 100 | 100 |
Joseph | 120 | 140 |
John | 90 | 150 |
Joseph | 20 | 140 |
John | 50 | 150 |
Sammy | 30 | 50 |
Goal is to select the sum of coupon prices for distinct customer, not alter the rows so we cannot use group by in main query, also the total coupon price should be same for all similar names.
Edit : I don't want to modify the table, I want this column while running select query, run time temp column.
SELECT Name,
coupon_price,
total_coupon_amount
FROM t1
JOIN (SELECT Name,
SUM(coupon_price) AS total_coupon_amount
FROM t1
GROUP BY Name) r2 USING (Name);
You create the sum in a subquery and join the two.
You should/could put also at the ORDER BY
what you want
If you want a view just out CREATE VIEW
before