<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>tsq Archives - Blog IT</title>
	<atom:link href="https://blogit.create.pt/tag/tsq/feed/" rel="self" type="application/rss+xml" />
	<link>https://blogit.create.pt/tag/tsq/</link>
	<description>Create IT blogger community</description>
	<lastBuildDate>Thu, 10 Jan 2019 12:46:19 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>Converting a Vertical Table to an Horizontal Table in SQL Server</title>
		<link>https://blogit.create.pt/ricardocosta/2016/10/27/converting-a-vertical-table-to-an-horizontal-table-in-sql-server/</link>
					<comments>https://blogit.create.pt/ricardocosta/2016/10/27/converting-a-vertical-table-to-an-horizontal-table-in-sql-server/#comments</comments>
		
		<dc:creator><![CDATA[Ricardo Costa]]></dc:creator>
		<pubDate>Thu, 27 Oct 2016 21:24:13 +0000</pubDate>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Sql]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[tsq]]></category>
		<guid isPermaLink="false">http://blogit.create.pt/ricardocosta/?p=1421</guid>

					<description><![CDATA[<p>Today I&#8217;ve encountered a vertical table in an SQL Database and I wanted to transform it to an horizontal one. A vertical table is described as an EAV model. Imagine you have this table If you run you get this Id Att_Id Att_Value 1 1 FirstName John 2 1 LastName Smith 3 1 Email john.smith@dummy.com [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/ricardocosta/2016/10/27/converting-a-vertical-table-to-an-horizontal-table-in-sql-server/">Converting a Vertical Table to an Horizontal Table in SQL Server</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Today I&#8217;ve encountered a vertical table in an SQL Database and I wanted to transform it to an horizontal one. A vertical table is described as an <a href="https://goo.gl/5aym8G" target="_blank" rel="noopener">EAV model</a>.</p>
<p>Imagine you have this table</p>
<pre class="brush: sql; title: ; notranslate">

CREATE TABLE VerticalTable
(
Id int,
Att_Id varchar(50),
Att_Value varchar(50)
)

INSERT INTO VerticalTable
SELECT 1, 'FirstName', 'John' UNION ALL
SELECT 1, 'LastName', 'Smith' UNION ALL
SELECT 1, 'Email', 'john.smith@dummy.com' UNION ALL
SELECT 2, 'FirstName', 'Jack' UNION ALL
SELECT 2, 'LastName', 'Daniels' UNION ALL
SELECT 2, 'Email', 'jack.daniels@dummy.com'

</pre>
<p>If you run</p>
<pre class="brush: sql; title: ; notranslate">

SELECT * FROM VerticalTable

</pre>
<p>you get this</p>
<table class="sqloutput">
<tbody>
<tr>
<th></th>
<th>Id</th>
<th>Att_Id</th>
<th>Att_Value</th>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>FirstName</td>
<td>John</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>LastName</td>
<td>Smith</td>
</tr>
<tr>
<td>3</td>
<td>1</td>
<td>Email</td>
<td>john.smith@dummy.com</td>
</tr>
<tr>
<td>4</td>
<td>2</td>
<td>FirstName</td>
<td>Jack</td>
</tr>
<tr>
<td>5</td>
<td>2</td>
<td>LastName</td>
<td>Daniels</td>
</tr>
<tr>
<td>6</td>
<td>2</td>
<td>Email</td>
<td>jack.daniels@dummy.com</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>To convert this into an horizontal table I&#8217;m going to use <a href="https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx" target="_blank" rel="noopener">PIVOT</a>.</p>
<pre class="brush: sql; title: ; notranslate">

SELECT &#x5B;Id], &#x5B;FirstName], &#x5B;LastName], &#x5B;Email] 
FROM
(
 SELECT Id, Att_Id, Att_Value FROM VerticalTable
) as source
PIVOT
(
 MAX(Att_Value) FOR Att_Id IN (&#x5B;FirstName], &#x5B;LastName], &#x5B;Email])
) as target

</pre>
<p>And I will get this</p>
<table class="sqloutput">
<tbody>
<tr>
<th></th>
<th>Id</th>
<th>FirstName</th>
<th>LastName</th>
<th>Email</th>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>John</td>
<td>Smith</td>
<td>john.smith@dummy.com</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>Jack</td>
<td>Daniels</td>
<td>jack.daniels@dummy.com</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>You can find the code <a href="http://rextester.com/NSKR13741" target="_blank" rel="noopener">here</a>.</p>
<p>The post <a href="https://blogit.create.pt/ricardocosta/2016/10/27/converting-a-vertical-table-to-an-horizontal-table-in-sql-server/">Converting a Vertical Table to an Horizontal Table in SQL Server</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/ricardocosta/2016/10/27/converting-a-vertical-table-to-an-horizontal-table-in-sql-server/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
