sql server - Adding columns to view in T-SQL using while loop -
i'm cleaning few queries in sql views.
in 1 view number of columns generated using following syntax
sum(case when navn = '000100' vaerdi else 0 end) '000100', sum(case when navn = '000110' vaerdi else 0 end) '000110', sum(case when navn = '000115' vaerdi else 0 end) '000115',
the list goes on , becomes quite extensive i'd put while loop.
something like
while (i in ('000100','000110','000115') sum(case when navn = vaerdi else 0 end) i, end
any suggestions on how accomplish this?
found solution!
using pivot function, able accomplish goal
with datatable ( select * dbo.table navn in ('000100', '000110') ) select * datatable pivot ( sum(vaerdi) for[navn] in (['000100],[000110]) ) p
Comments
Post a Comment