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

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 )

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.