sql server - SQL - If two records have same value in two columns, add third column -
i have table these values:
col1 | col2 | col3 | 1 | 2 | 3 | 4 | 5 | 6 | 4 | 7 | 8 | 4 | 7 | 9 |
i want make query returns row every record in col1 , col2 both different in every other record, if both same in 2 records, result should 1 single row values in col1 , col2 , col3 sum in third column. result should be:
col1 | col2 | colnew | 1 | 2 | 3 | 4 | 5 | 6 | 4 | 7 | 17 |
thank you.
use group by
on columns want "group by" (1 , 2 in case). use sum
on column want "sum" (3 in case).
select a.col1, a.col2, sum(a.col3) colnew test group a.col1, a.col2;
sqlfiddle: http://sqlfiddle.com/#!3/070a3/4
Comments
Post a Comment