I am working on a SQL Query. Query is something like below:
Select
a.field1,
b.field2,
c.field3,
c.field4,
b.filed5,
a.field6,
d.field7
from
a
Inner join b on a.field1 = b.field1
right join c on b.field2 = c.field3
left join d on d.filed3 = a.field1
where some conditions;
The output of the above would be something like this:
field1 | field2 | field3 | field4 | field5 | field6 | field7
--------------------------------------------------------------
name | value1 | other1 | 1 | diff | new | 100
name1 | value2 | other2 | 1 | diff1 | new1 | 100
name2 | value3 | other3 | 2 | diff2 | new2 | 100
So i need a new column in the result which sums the field7
based on field4
value(if they are same).
Is it in possible in SQL? I tried to use Group by field 4
here but I am getting an error that field1
should be used in group by. So i am not able get the result as expected.
Expected Result:
field1 | field2 | field3 | field4 | field5 | field6 | field7
--------------------------------------------------------------
name | value1 | other1 | 1 | diff | new | 200
some | value3 | other3 | 2 | diff2 | new2 | 100
Basically i want to remove one column based on a condition and sum the last field.
Any suggestions are helpful.