sql server - TSQL - UNPIVOT from Excel imported data -
i have excel spreadsheet imports table so:
+-------------------------------------------------------------------------+ | col1 col2 col3 col4 col5 | +-------------------------------------------------------------------------+ | ref name 01-01-2013 02-01-2013 03-01-2013 | | 1 john 500 550 600 | | 2 fred 600 650 400 | | 3 paul 700 750 550 | | 4 steve 800 850 700 | +-------------------------------------------------------------------------+
my goal change this:
+-------------------------------------------------------------------------+ | ref name date sales | +-------------------------------------------------------------------------+ | 1 john 01-01-2013 500 | | 1 john 02-02-2013 550 | | 1 john 03-01-2013 600 | | 2 fred 01-01-2013 600 | | ..... | +-------------------------------------------------------------------------+
so far figured out how use unpivot dates , sales numbers 1 column doesn't solve problem of breaking dates out own column. appreciated. thanks!!
you possibly use 2 separate unpivot queries , join them. first unpivot, convert row ref
value in col1
, second subquery unpivot of sales
. join subqueries on previous column names:
select s.col1, s.col2, d.value date, s.value sales ( select col1, col2, col, value yt unpivot ( value col in (col3, col4, col5) ) un col1 = 'ref' ) d inner join ( select col1, col2, col, value yt unpivot ( value col in (col3, col4, col5) ) un col1 <> 'ref' ) s on d.col = s.col;
see sql fiddle demo
Comments
Post a Comment