Minimum coin change Problem

Here in this code if I take denomination of coin in outer loop, it is working.
But If I want to take sum in outer loop, and consider each denomination (Basically filling the table in a different manner). The code does not work. Can you please share the working code in which I take sum outside and denominations inside? Also which approach to follow and how to decide that?
Thanks.
for(i=1;i<n+1;i++)
{
for(j=1;j<sum+1;j++)
{
if(a[i-1]>j)
set[i][j]=set[i-1][j];
else
set[i][j]=min(set[i-1][j],set[i][j-a[i-1]]+1);
}
}
return set[n][sum];