sql server - Unpivot SQL statement -
select assylotno, altlotname, wfrnodisplay, param.display, param.value emap_data unpivot (value display in (altlotnumber, wfrnumber, needspeedinfo, maptype, buildlist, type, productgrade, assylotno) ) param
objective of query above have assylotno field included in display , value columns. fields exists in table emap_data, however, keep getting invalid column name field assylotno exists in emap_data.
emap_data table fields assylotno | altlotname | altlotnumber | wfrnodisplay | wfrnumber | needspeedinfo | maptype | buildlist | type | productgrade desired output follow: assylotno altlotname wfrnodisplay display value ------------------------------------------------------------------ abc1231 abc123 01 altlotname abc123 abc1231 abc123 01 wfrnumber 01 abc1231 abc123 01 needspeedinfo y abc1231 abc123 01 maptype 12 abc1231 abc123 01 buildlist 1,2,3,4,5,6 abc1231 abc123 01 type s abc1231 abc123 01 productgrade c abc1231 abc123 01 assylotno abc1231
ms sql server 2012 schema setup:
create table emap_data ( wfrnodisplay varchar(20), altlotname varchar(20), wfrnumber varchar(20), needspeedinfo varchar(20), maptype varchar(20), buildlist varchar(20), type varchar(20), productgrade varchar(20), assylotno varchar(20) ) insert emap_data values ('01', 'abc123', '01','y','12','1,2,3,4,5,6','s','c','abc1231')
query 1:
select e.assylotno, e.altlotname, e.wfrnodisplay, t.display, t.value emap_data e cross apply (values(altlotname, 'altlotname'), (wfrnumber, 'wfrnumber'), (needspeedinfo, 'needspeedinfo'), (maptype, 'maptype'), (buildlist, 'buildlist'), (type, 'type'), (productgrade, 'productgrade'), (assylotno, 'assylotno') ) t(value, display)
| assylotno | altlotname | wfrnodisplay | display | value | |-----------|------------|--------------|---------------|-------------| | abc1231 | abc123 | 01 | altlotname | abc123 | | abc1231 | abc123 | 01 | wfrnumber | 01 | | abc1231 | abc123 | 01 | needspeedinfo | y | | abc1231 | abc123 | 01 | maptype | 12 | | abc1231 | abc123 | 01 | buildlist | 1,2,3,4,5,6 | | abc1231 | abc123 | 01 | type | s | | abc1231 | abc123 | 01 | productgrade | c | | abc1231 | abc123 | 01 | assylotno | abc1231 |
Comments
Post a Comment