Unpivot and Pivot does not return data -
i'm trying return data columns.
i've written unpivot , pivot query:
`select stockitemcode, barcode, barcode2 (select stockitemcode, col+cast(seq varchar(20)) col, value ( select (select min(stockitemcode) rtlbarcode t2 t.stockitemcode = t2.stockitemcode) stockitemcode, cast(barcode varchar(20)) barcode, row_number() over(partition stockitemcode order stockitemcode) seq rtlbarcode t) d unpivot( value col in (barcode) ) unpiv) src pivot ( max(value) col in (barcode, barcode2)) piv;`
but problem "barcode2" field returning value (the barcode field returns null when in fact there value.
sample data
i have table called rtlbarcode has field called barcode , field called stockitemcode
for stockitemcode = 10 have 2 rows barcode value of 5014721112824 , 0000000019149.
can see going wrong?
many thanks
you indexing barcode in unpiv. results in col
's-values barcode1
, barcode2
.
but pivoting on barcode
instead of barcode1
. no value found , aggregate returns null
.
the correct statement be:
select stockitemcode, barcode1, barcode2 ( select stockitemcode, col+cast(seq varchar(20)) col, value ( select (select min(stockitemcode)from rtlbarcode t2 t.stockitemcode = t2.stockitemcode) stockitemcode, cast(barcode varchar(20)) barcode, row_number() over(partition stockitemcode order stockitemcode) seq rtlbarcode t ) d unpivot(value col in (barcode)) unpiv ) src pivot (max(value) col in (barcode1, barcode2)) piv
Comments
Post a Comment