pivot - How to transform SQL Server row data to columns? -
i queried data this
member_no cover_version product_id product_name product_type --------- ------------- ---------- -------------------- ------------ 11421 7 4 excellent more e 11421 7 15 comprehensive data d
but want shape data this:
member_no cover_version product_e_id product_e_name product_d_id product_d_name --------- ------------- ------------ -------------------- ------------ -------------------- 11421 7 4 excellent more 15 comprehensive data
i using sql server 2008. should best approach shape data want?
assuming you've got product types d , e stated, simple self-join you're after.
if want more generic, please expand question.
drop table if exists #demo select * #demo (values (11421, 7, 4, 'excellent more', 'e') ,(11421, 7, 15, 'comprehensive data', 'd')) (member_no, cover_version, product_id, product_name, product_type) select d.member_no ,d.cover_version ,e.product_id product_e_id ,e.product_name product_e_name ,d.product_id product_d_id ,d.product_name product_d_name #demo d join #demo e on d.member_no = e.member_no , d.product_type = 'd' , e.product_type = 'e'; member_no cover_version product_e_id product_e_name product_d_id product_d_name ----------- ------------- ------------ ------------------ ------------ ------------------ 11421 7 4 excellent more 15 comprehensive data
Comments
Post a Comment