I prefer t-clausen's answer , but as an exercise, I decided to try it as UNPIVOT followed by PIVOT so that we can write it using more common SQL tools:
declare @t table (SomeID int,Cash money,Credit money,Debit money,Account money,Other money) insert into @t(SomeID,Cash,Credit,Debit,Account,Other) values (1,0.00,0.00,0.00,0.00,0.00), (2,1.00,0.00,0.00,0.00,0.00), (3,2.00,1.00,0.00,0.00,0.00) ;With Unpiv as ( select *,SUM(CASE WHEN MoneyValue > 0.00 THEN 1 ELSE 0 END) OVER (PARTITION BY SomeID) as cnt from @tt unpivot (MoneyValue for MoneyType in (Cash,Credit,Debit,Account,Other)) x ), Repiv as ( select * from Unpiv u pivot (SUM(MoneyValue) for MoneyType in (Cash,Credit,Debit,Account,Other)) x where cnt >= 2 ) select * from Repiv
This assumes that you have another column (called SomeID here), by which each row can be uniquely identified.
Result:
SomeID cnt Cash Credit Debit Account Other ----------- ----------- --------------------- --------------------- --------------------- --------------------- --------------------- 3 2 2.00 1.00 0.00 0.00 0.00
Just hoping that the above can be more adapted to some of the requirements.