SQL Server Row to Cols -
this question has answer here:
i seems simple task need sql experts one.
the number of contacts can vary 1 30 persons , want phone numbers in 1 row per cust_ref.
how can this??
this solution without dynamic sql, please try out:
declare @contacts table(cust_ref int,phone varchar(100)); insert @contacts values(10000,'ph10000a'),(10000,'ph10000b'),(10000,'ph10000c') ,(10001,'ph10001a'),(10001,'ph10001b'),(10001,'ph10001c') ,(10002,'ph10002a'),(10002,'ph10002b'); select p.* ( select 'phone'+replace(str(row_number() over(partition cust_ref order phone),2),' ','0') inx,* @contacts ) tbl pivot ( min(phone) inx in(phone01,phone02,phone03,phone04) --type more... ) p
Comments
Post a Comment