Hi,
I have a table with columns like these :
idx | amount | usercol1 | usercol2 | usercol3 | percentage1 | percentage2 | percentage3
Data is typically like this :
0 | 1500 | 1 | null | null | 100 | null | null
1 | 3000 | 2 | 3 | null | 50 | 50 | null
I would like to make a SUM() of every user's amount.
Example :
- user1= 1500*100/100 (amount*usercol1/100)
- user2= 3000*50/100 (amount*usercol1/100)
- user3= 3000*50/100 (amount*usercol2/100)
I tried UNION to no avail (did not sum the SUMs).
Is there a way to do this ? The problem being that it should GROUP BY the username (which I get with a LEFT OUTER JOIN usernames ON exampletable.usercol1=usernames.idx).
I know this is non standard and would be better with relations from another table. But I am not allowed to change the table structure.
Many many many thanks ! :=)
Hereby, an example that gives a wrong result (seems to give only results from the query in the middle)
(
SELECT SUM(projects.amount * (projects.percentage1/100)) as totalproj,
entities.idx as idx,
COUNT(projects.idx) as numproj,
entities.name
FROM projects
INNER JOIN entities ON projects.usercol1=entities.idx
WHERE projects.usercol1=entities.idx
GROUP BY name ORDER BY totalproj DESC
)
UNION ALL
(
SELECT SUM(projects.amount * (projects.percentage2/100)) as totalproj,
entities.idx as idx,
COUNT(projects.idx) as numproj,
entities.name
FROM projects
INNER JOIN entities ON projects.usercol2=entities.idx
WHERE projects.usercol2=entities.idx
GROUP BY name ORDER BY totalproj DESC
)
UNION ALL
(
SELECT SUM(projects.amount * (projects.percentage3/100)) as totalproj,
entities.idx as idx,
COUNT(projects.idx) as numproj,
entities.name
FROM projects
INNER JOIN entities ON projects.usercol3=entities.idx
WHERE projects.usercol3=entities.idx
GROUP BY name ORDER BY totalproj DESC
)
ORDER BY totalproj DESC
LIMIT 10