Thursday, August 6, 2009

Converting Rows to Columns using PIVOT - SQL scripts






select * from t1


select * from t2



-- query for table iii


select ITEMNMBR,currcost,[A] as A, [B] as B,[I] as I
from
(select IV1.ITEMNMBR as itemnmbr,
--iv1.ITEMDESC as itemdesc,
currcost,
substring(iv2.locncode,7,1) as locncode,QTYONHND
from t1 iv1 join t2 iv2 on iv1.itemnmbr = iv2.itemnmbr where QTYONHND <> 0) ps
PIVOT
(
sum(QTYONHND)
for locncode in
([A],[B],[I])
) as pvt



No comments:

Post a Comment