<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://sqlzoo.net/w/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://sqlzoo.net/w/index.php?title=Data_normalization&amp;feed=atom&amp;action=history</id>
		<title>Data normalization - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://sqlzoo.net/w/index.php?title=Data_normalization&amp;feed=atom&amp;action=history"/>
		<link rel="alternate" type="text/html" href="http://sqlzoo.net/w/index.php?title=Data_normalization&amp;action=history"/>
		<updated>2013-05-19T03:28:20Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.20.4</generator>

	<entry>
		<id>http://sqlzoo.net/w/index.php?title=Data_normalization&amp;diff=1464&amp;oldid=prev</id>
		<title>Marek: Created page with &quot;To select the data from 20 different columns into one changing the values according to one other column consisting the positions of the 20 columns. &lt;div class='ht'&gt; &lt;div class...&quot;</title>
		<link rel="alternate" type="text/html" href="http://sqlzoo.net/w/index.php?title=Data_normalization&amp;diff=1464&amp;oldid=prev"/>
				<updated>2012-07-17T11:32:58Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;To select the data from 20 different columns into one changing the values according to one other column consisting the positions of the 20 columns. &amp;lt;div class=&amp;#039;ht&amp;#039;&amp;gt; &amp;lt;div class...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;To select the data from 20 different columns into one changing the values according to one other column consisting the positions of the 20 columns.&lt;br /&gt;
&amp;lt;div class='ht'&amp;gt;&lt;br /&gt;
&amp;lt;div class=params&amp;gt;schema:scott&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
Sometimes we have un-normailsed data that we want to normalise.&lt;br /&gt;
Suppose we have data in 20 columns F1 to F20:&lt;br /&gt;
&amp;lt;pre&amp;gt;Line    F1    F2   F3   F4 ...&lt;br /&gt;
------------------------------&lt;br /&gt;
A       11    10   13   15 ...&lt;br /&gt;
B       20    22   23   28 ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
But we want a table that has just one data column. Like this...&lt;br /&gt;
&amp;lt;pre&amp;gt;Line   Col   Val&lt;br /&gt;
----------------&lt;br /&gt;
A      1     11&lt;br /&gt;
A      2     10&lt;br /&gt;
A      3     13&lt;br /&gt;
A      4     15&lt;br /&gt;
...&lt;br /&gt;
B      1     20&lt;br /&gt;
B      2     22&lt;br /&gt;
B      3     23&lt;br /&gt;
B      4     28&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can use INSERT ... SELECT ... statement&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=sql class='tidy'&amp;gt;DROP TABLE normal&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;source lang=sql class='setup'&amp;gt;DROP TABLE unnormal;&lt;br /&gt;
CREATE TABLE unnormal&lt;br /&gt;
  (Line CHAR(1) PRIMARY KEY,&lt;br /&gt;
   F1 INTEGER,&lt;br /&gt;
   F2 INTEGER,&lt;br /&gt;
   F3 INTEGER,&lt;br /&gt;
   F4 INTEGER);&lt;br /&gt;
--Put the bad data in&lt;br /&gt;
INSERT INTO unnormal VALUES&lt;br /&gt;
 ('A', 11, 10, 13, 15);&lt;br /&gt;
INSERT INTO unnormal VALUES&lt;br /&gt;
 ('B', 20, 22, 23, 28);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang='sql' class='def'&amp;gt;--Create the good table&lt;br /&gt;
CREATE TABLE normal&lt;br /&gt;
 (Line CHAR(1), col INTEGER, val INTEGER,&lt;br /&gt;
  PRIMARY KEY (Line, col) );&lt;br /&gt;
--Copy the data into it&lt;br /&gt;
INSERT INTO normal(Line, col, val)&lt;br /&gt;
  SELECT Line, 1, F1 FROM unnormal&lt;br /&gt;
UNION&lt;br /&gt;
  SELECT Line, 2, F2 FROM unnormal&lt;br /&gt;
UNION&lt;br /&gt;
  SELECT Line, 3, F3 FROM unnormal&lt;br /&gt;
UNION&lt;br /&gt;
  SELECT Line, 4, F4 FROM unnormal;&lt;br /&gt;
SELECT * FROM normal;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;ecomm e-mysql&amp;quot; style=&amp;quot;display: none&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{INSERT and DELETE ref}}&lt;/div&gt;</summary>
		<author><name>Marek</name></author>	</entry>

	</feed>