i am in urgent need of some help.
consider the following table:
col1, col2, col3
1, 1.25, 1
1, 1.25, 2
1, 1.25, 2
2, 0.75, 1
2, 0.75, 1
2, 0.75, 1
3, 1.25, 1
3, 1.25, 1
3, 1.25, 1
3, 1.25, 2
from this "theoretical" SQL statement
SELECT col3, IF(DISTINCT(col1),SUM(col2),SUM(0)) As mySum FROM table1 GROUP BY col3
or perhaps this one (neither actually works)
SELECT col3, SUM(IF(DISTINCT(col1), col2, 0)) As mySum FROM table1 GROUP BY col3
result would be...
col3, mySum
1, 3.25
2, 2.50
i have a solution that invoves sub-queries but it is too slow (so please don't suggest that route). is there some little bit of syntactic logic i'm missing to get my "if" clause to work?
one more thing: would it be easy enough to write a UDF to handle this?
consider the following table:
col1, col2, col3
1, 1.25, 1
1, 1.25, 2
1, 1.25, 2
2, 0.75, 1
2, 0.75, 1
2, 0.75, 1
3, 1.25, 1
3, 1.25, 1
3, 1.25, 1
3, 1.25, 2
from this "theoretical" SQL statement
SELECT col3, IF(DISTINCT(col1),SUM(col2),SUM(0)) As mySum FROM table1 GROUP BY col3
or perhaps this one (neither actually works)
SELECT col3, SUM(IF(DISTINCT(col1), col2, 0)) As mySum FROM table1 GROUP BY col3
result would be...
col3, mySum
1, 3.25
2, 2.50
i have a solution that invoves sub-queries but it is too slow (so please don't suggest that route). is there some little bit of syntactic logic i'm missing to get my "if" clause to work?
one more thing: would it be easy enough to write a UDF to handle this?