Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friends
I am trying to write a query to run in ms access table from vb.net. I have a table with three columns col1,col2 and col3. there are no more than two rows with the same letter in col1.I would like to know how do I subtract columns based on column 1 ? for summing column based on condition I can use a query like this

SQL
select col1, SUM(col2) as col2, SUM(col3) as col3 from table1 group by col1 



table1
col1|col2|col3|
------|---|----|
A |10 | 12 |
B | 4 | 6 |
A | 7 | 2 |

output
col1|col2 |col3
----|----|----|
A | 3 | 10 |
B | 4 | 6 |

Circular reference caused by alias 'col2' in query definition's SELECT list.
SQL
"select col1, MAX(col2) *2 - SUM(col2) as col2,MAX(col3) * 2 - SUM(col3) as col3   from table1 group by col1"



Thanks
Posted

1 solution

You can workaround it by using subquery:

SQL
SELECT col1, MAX(col2)*2-col2 as col2, MAX(col3)*2-col3 as col3
FROM ( 
    select col1, SUM(col2) as col2, SUM(col3) as col3
    from table1
    group by col1 
) AS T
GROUP BY col1;
 
Share this answer
 
Comments
josef088 11-Nov-14 1:50am    
thanks
Maciej Los 11-Nov-14 4:17am    
You're welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900