//first UNPIVOT table with one column [a] for [a1] [a2] [a3]
select [name],[Pincode][a],identity(int,1,1) as Pk
into #Temp1 from
(select tbl1.[Pincode],tbl1.[name],tbl1.[a1],tbl1.[a2],tbl1.[a3] from tablename as tbl1) as up
UNPIVOT
([a] for [tempval] in ([a1],[a2],[a3])
) as pvt
// second UNPIVOT table with one column [b] for [b1] [b2] [b3]
select [Pincode],[name],[b],IDENTITY(int,1,1) as pk into #Temp2
from
(select tbl2.[Pincode],tbl2.[name],tbl2.[b1],tbl2.[b2],tbl2.[b3] from tablename as tbl2) as up
UNPIVOT
([b] for[tempval] in ([b1],[b2],[b3])
)
as unpvt
// this will br the output of the both above table
select temptable1.pk,temptable1.[name],temptable1.[pincode],temptable1.a,temptable2.b,
into #Temp3 from #Temp1 temptable1
inner join #Temp2 temptable2 on temptable1.pk = temptable2.pk
//the last tabll has the proper record "#Temp3"