0

Hi Guys,

I'm having an issue with my sum() function on my queries.

My DB has 3 tables
1. plans
2. handset
3. matrix

plans;
- planNum
- planCode
- planName
- planLength
- simRebate
- commission

handset;
- handset
- RRP

matrix;
- planNum
- handset
- subsidy

The query that I'm trying to run is as follows;

SELECT planCode, handset, planName, RRP, simRebate, commission, subsidy, SUM( subsidy + simRebate + commission - RRP ) AS total
FROM plans
RIGHT JOIN (
vhandsetmatrix
LEFT JOIN vodahandsets
USING ( handset ) 
)
USING ( planNum )

However, I would like to run the above SUM() on each row rather than on the whole table.


Help would be much appreciated.

Thanks

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by tyson.crouch
0

Don't use the sum() function:

SELECT planCode, handset, planName, RRP, simRebate, commission, subsidy, 
subsidy + simRebate + commission - RRP AS total
FROM plans
RIGHT JOIN (
vhandsetmatrix
LEFT JOIN vodahandsets
USING ( handset ) 
)
USING ( planNum )
0

I have solved this issue using GROUP BY.

Final solution;

SELECT planCode, handset, planName, RRP, simRebate, commission, subsidy, SUM( subsidy + simRebate + commission ) AS total
FROM plans
RIGHT JOIN (
vhandsetmatrix
LEFT JOIN vodahandsets
USING ( handset ) )
USING ( planNum ) 
GROUP BY planName, handset
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.