I need to constract an SQL query but I have no idea how to do it. If someone helps, I’ll appriciate it very much.
I have the following table
GroupedBYField ConditionField ToBeSummeField
1 1 1
1 1 2
1 1 3
2 2 100
2 2 200
2 2 300
and I need to get all the possible combinations of groupedBYField, SUM(ToBeSummeField) which has
SUM(conditionField) = 2, that is the following table
GroupedBYField SumField
1 3
1 4
1 5
2 100
2 200
2 300
Thank you for your help!
I believe this works. It should also work where ConditionField values of 0 appear.
It will run on SQL 2005/2008.
It uses a recursive CTE to deal with any number of potential rows adding to the required value
EDIT – added maxrecursion 0 to permit this to work on any number of source rows